Listening for variable changes in JavaScript

后端 未结 22 2848
自闭症患者
自闭症患者 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:33

    The functionality you're looking for can be achieved through the use of the "defineProperty()" method--which is only available to modern browsers:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

    I've written a jQuery extension that has some similar functionality if you need more cross browser support:

    https://github.com/jarederaj/jQueue

    A small jQuery extension that handles queuing callbacks to the existence of a variable, object, or key. You can assign any number of callbacks to any number of data points that might be affected by processes running in the background. jQueue listens and waits for these data you specify to come into existence and then fires off the correct callback with its arguments.

提交回复
热议问题