How can I attach a change event handler to a variable?

Viewed 16659

Possible Duplicate:
Listening for variable changes in JavaScript or jQuery

How can I track if this variable has changed?

var ConditionalFlag = 0;

I tried this:

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

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++;

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

2 Answers
Related