jQuery 在变量更改时触发 [英] jQuery trigger on variable change

查看:21
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆