0

Linked Listening for variable changes in JavaScript

I want to run callback() only if the value is changed instead of set. If it is set to the same value as previous one, callback() is not fired. If it is set to different value then previous one, it is changed and callback() is run.

How do I do this? I tried storing it in prevVariable and currentVariable, and comparing them with if statement. But I was wondering if there is a better JS feature that does what I want? The variable I am receiving is from external source that I cannot control and I am currently running it with setTimeout to get constant update of the currentVariable.

Ghost
  • 749
  • 4
  • 15
  • 2
    You haven't shown your code, but the general idea you describe seems reasonable. If you have to keep track of lots of them, use an object whose keys are the variable names. – Barmar Jun 26 '20 at 04:59
  • @Karizma why did you delete your answer? – Nick Jun 26 '20 at 05:19

0 Answers0