2017-04-13 28 views
0

我正在創建一個應用程序,用戶可以根據輸入獲得獎盃。我想在獲得獎盃時提醒用戶。我不確定如何寫:關於布爾變化(JavaScript)的火事件

var trophy = false 
if (userInput) { 
    trophy = true 
} 

// alert user when boolean changes from false to true after userInput 

所有的解決方案我想出提醒用戶當布爾值爲true(這是每一個實例變更後),但我只是想在變化的一次提醒。布爾值只會翻轉一次;一旦這是真的,它永遠不會回到錯誤。

謝謝!

+0

看到這個線程:http://stackoverflow.com/questions/1029241/object-watch-for-all-browsers的 – codechurn

+0

可能的複製[Object.watch()爲所有瀏覽器?](http://stackoverflow.com/questions/1029241/object-watch-for-all-browsers) – codechurn

回答

0

您可以檢查的價值你改變它之前:

var trophy = false 
if (userInput) { 
    if (!trophy) { 
     // alert user here 
    } 
    trophy = true 
} 
+0

這正是我正在尋找。謝謝! – mkhira2