jQuery触发变量更改 [英] jQuery trigger on variable change
本文介绍了jQuery触发变量更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个设置,可以根据需要加载许多模块,更改特定变量后,每个模块都有特定的加载需求.我需要像jquery触发器之类的东西,但是当变量更改时它会运行,像这样:
I have a setup where i load many modules as required, each module has a specific load needs when a specific variable is changed. I need something like jquery trigger but that runs when a variable change, something like this:
var x = 0; // no triggers
// something happens
x = 1; // will trigger a function
x = 2; // will trigger the same function
谢谢.
推荐答案
也许您可以创建一个包含set_x
方法的对象来更改变量的值.除了更改变量之外,此方法还可以调用任何关联的触发器.
Perhaps you could create an object that contains a set_x
method to change the value of the variable. In addition to changing the variable, this method could call any associated triggers.
然后,您可以使用此新方法来代替直接设置x
:
Then instead of setting x
directly, you would use this new method:
my_obj.set_x( 2 ); // Will trigger a function
这篇关于jQuery触发变量更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文