如何将变更事件处理程序附加到变量? [英] How can I attach a change event handler to a variable?

查看:63
本文介绍了如何将变更事件处理程序附加到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

监听JavaScript或jQuery中的变量

如何跟踪此变量是否已更改?

How can I track if this variable has changed?

var ConditionalFlag = 0;

我试过这个:

var ConditionalFlag = 0;
$(ConditionalFlag).change(function () {
    alert("Changed");
});
ConditionalFlag++;

但无济于事。我考虑使用25ms计时器来检查这样的变化:

But to no avail. I have considered using a 25ms timer to check for change like this:

var ConditionalFlag = 0;
function CheckFlag() {
    if (ConditionalFlag > 0) {
        alert("Changed");
        clearInterval(check);
    }
}
var check = window.setInterval("CheckFlag()", 25);
ConditionalFlag++;

然而,这似乎有点矫枉过正。有没有办法用jQuery或javascript将事件处理程序附加到此变量?

However, that seems like overkill. Is there a way to attach an event handler to this variable with jQuery or javascript?

推荐答案

没有事件被触发变量变化时JavaScript无法正常工作。

There's no "event" that gets triggered when a variable changes. JavaScript doesn't work that way.

此变量何时更改?只需在函数之后添加对函数的调用。

When does this variable get changed? Just add a call to a function after it does.

这篇关于如何将变更事件处理程序附加到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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