Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call JavaScript's setTimeOut in dojo Class

I'm trying to convert my JavaScript functions to a dojo Class. I've a setTimeOut("functionName",2000) in one of my JS method. How do I call this from a method in the class decared using dojo.declare method. For example, below is my custom class.

    dojo.declare("Person",null,{
                    constructor:function(age,country,name,state){
                        this.age=age;
                        this.country=country;
                        this.name=name;
                        this.state=state;
                    },
                    moveToNewState:function(newState){
                        this.state=newState;
//I need to call "isStateChanged" method after 2000 ms. How do I do this?
                        setTimeOut("isStateChanged",2000);
                    },                  
                    isStateChanged:function(){
                        alert('state is updated');
                    }
                });
var person=new Person(12,"US","Test","TestState");
person.moveToNewState("NewState");

Please let me know how I can call isStateChanged method from moveToNewState method after 2000ms.

like image 320
Steven Avatar asked Feb 23 '11 14:02

Steven


3 Answers

What you're looking for is a way of binding the this value to the function that setTimeout will call:

moveToNewState:function(newState){
    // Remember `this` in a variable within this function call
    var context = this;

    // Misc logic
    this.state = newState;

    // Set up the callback
    setTimeout(function() {
        // Call it
        context.isStateChanged();
    }, 2000);
},     

The above is using a closure to bind the context (see: Closures are not complicated), which is the usual way to do this. Dojo may offer a built-in function for generating these "bound" callbacks (Prototype and jQuery do). (Edit: It does, in his comment below peller kindly pointed out dojo.hitch.)

More about this general concept here: You must remember this

like image 175
T.J. Crowder Avatar answered Nov 16 '22 15:11

T.J. Crowder


This has nothing to do with dojo, this is pure javascript. What you're looking for is:

var $this = this;
setTimeout(function() { $this.isStateChanged() }, 2000);

Check out the docs on setTimeout.

Oh, and, please don't use quotes around your function names (because that makes it a useless string that will probably get evaled and will give an error).

like image 41
Felix Avatar answered Nov 16 '22 17:11

Felix


You could simply call setTimeout(this.isStateChanged, 2000) which will pass the correct function reference, not unlike the way you would call the method directly. The expression this.isStateChanged is evaluated immediately. To make the call, there's no need to wrap it in an extra function or declare local variables.

To bind the this variable to the called function, you can use dojo.hitch which will create its own closure without polluting the local variable space and potentially leaking through other references.

like image 2
peller Avatar answered Nov 16 '22 15:11

peller