微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在dojo Class中调用JavaScript的setTimeOut

我正在尝试将我的 JavaScript函数转换为dojo类.我在我的一个JS方法中有一个setTimeOut(“functionName”,2000).如何使用dojo.declare方法从decared类中的方法调用它.例如,下面是我的自定义类.
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");

请告诉我如何在2000ms后从movetoNewState方法调用isstateChanged方法.

您正在寻找的是一种将此值绑定到setTimeout将调用函数方法
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);
},

以上是使用闭包来绑定上下文(参见:Closures are not complicated),这是执行此操作的常用方法. Dojo可以提供内置函数生成这些“绑定”回调(Prototype和jQuery do). (编辑:确实如此,他在peller以下的评论中指出了dojo.hitch.)

关于这个一般概念的更多信息:You must remember this

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐