4

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

Thanks.

3
  • You can do exactly this...there's nothing you can listen to, can you give a bit more context? Commented Jul 16, 2010 at 18:20
  • Duplicate of this: stackoverflow.com/questions/1759987/… Commented Jul 16, 2010 at 18:22
  • that's a duplicate i'm closing it, thx. Commented Jul 16, 2010 at 20:00

2 Answers 2

5

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.

Then instead of setting x directly, you would use this new method:

my_obj.set_x( 2 ); // Will trigger a function
Sign up to request clarification or add additional context in comments.

Comments

4

How about this?

var x = {
    value: 10
}

var set = function(obj, new_value) {
    obj.value = new_value;
    /* call whatever your heart desires here */
}

So you can do:

set(x, 2);
// now x.value = 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.