0

I am creating an app where users earn trophies based on input. I want to alert the user when they earn a trophy. I'm uncertain how to write:

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

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

All the solutions I come up with alert the user when the boolean is true (which is every instance after the change), but I just want to alert once on change. The boolean will only ever flip once; once it's true, it never goes back to false.

Thanks!

mkhira2
  • 564
  • 1
  • 7
  • 14
  • See this thread: http://stackoverflow.com/questions/1029241/object-watch-for-all-browsers – codechurn Apr 13 '17 at 19:36
  • Possible duplicate of [Object.watch() for all browsers?](http://stackoverflow.com/questions/1029241/object-watch-for-all-browsers) – codechurn Apr 13 '17 at 19:36

1 Answers1

0

You can check the value just before you change it:

var trophy = false
if (userInput) {
    if (!trophy) {
        // alert user here
    }
    trophy = true
}
thedude
  • 9,388
  • 1
  • 29
  • 30