Listening for variable changes in JavaScript

后端 未结 22 2788
自闭症患者
自闭症患者 2020-11-21 06:57

Is it possible to have an event in JS that fires when the value of a certain variable changes? JQuery is accepted.

22条回答
  •  一生所求
    2020-11-21 07:22

    Please guys remember the initial question was for VARIABLES, not for OBJECTS ;)

    in addition to all answers above, I created a tiny lib called forTheWatch.js, that use the same way to catch and callback for changes in normal global variables in javascript.

    Compatible with JQUERY variables, no need to use OBJECTS, and you can pass directly an ARRAY of several variables if needed.

    If it can be helpful... : https://bitbucket.org/esabora/forthewatch
    Basically you just have to call the function :
    watchIt("theVariableToWatch", "varChangedFunctionCallback");

    And sorry by advance if not relevant.

提交回复
热议问题