在dojo类中调用JavaScript的setTimeOut

发布于 2024-10-18 20:30:29 字数 1050 浏览 6 评论 0原文

我正在尝试将 JavaScript 函数转换为 dojo 类。我的一个 JS 方法中有一个 setTimeOut("functionName",2000) 。如何从使用 dojo.declare 方法声明的类中的方法调用它。例如,下面是我的自定义类。

    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");

请告诉我如何在 2000 毫秒后从 moveToNewState 方法调用 isStateChanged 方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月无声 2024-10-25 20:30:29

您正在寻找的是将 this绑定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);
},     

上面使用的是 >closure 绑定上下文(参见:< em>闭包并不复杂),这是通常的方法。 Dojo 可能会提供一个内置函数来生成这些“绑定”回调(Prototype 和 jQuery 都是如此)。 (编辑:确实如此,在他下面的评论中peller善意地指出dojo.hitch。)

有关此一般概念的更多信息,请参见:你必须记住这个

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

慈悲佛祖 2024-10-25 20:30:29

这与dojo无关,这是纯javascript。您要查找的是:

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

查看有关 setTimeout 的文档

哦,还有,请不要在函数名称周围使用引号(因为这会使其成为无用的字符串,可能会被评估并给出错误)。

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).

爱的那么颓废 2024-10-25 20:30:29

您可以简单地调用 setTimeout(this.isStateChanged, 2000) ,它将传递正确的函数引用,这与直接调用该方法的方式不同。立即计算表达式 this.isStateChanged。要进行调用,无需将其包装在额外的函数中或声明局部变量。

要将 this 变量绑定到被调用的函数,可以使用 dojo.hitch 它将创建自己的闭包,而不会污染局部变量空间并可能通过其他引用泄漏。

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.

情独悲 2024-10-25 20:30:29

您可以使用 dojo/_base/lang 并使用其挂接方法,如下代码所示:

moveToNewState:function(newState){
  // Misc logic
  this.state = newState;

  // Set up the callback
  setTimeout(lang.hitch(this, function() {
    // Call with `this`
    this.isStateChanged();
  }), 2000);
}, 

You can use the dojo/_base/lang and use its hitch method like the below code:

moveToNewState:function(newState){
  // Misc logic
  this.state = newState;

  // Set up the callback
  setTimeout(lang.hitch(this, function() {
    // Call with `this`
    this.isStateChanged();
  }), 2000);
}, 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文