javascript - window.setInterval() 的 dojo.hitch() 范围

标签 javascript dojo scope

我正在尝试使用 dojo fadeIn/Out 产生闪烁效果。

下面的代码片段是在小部件类的声明中定义的:

 _startHighlightEffect : function() {
      var blinkInterval = 5000; //Scope here is that of the parent widget
      window.setInterval ( function() {
              dojo.fadeOut(
              {
                      node: this._headerDiv.domNode,
                      onEnd: function() {
                              dojo.fadeIn({node: this._headerDiv.domNode},3000).play();
                      }
              },3000).play();
      }, blinkInterval);
  },

_highlightEffect : function() {
    this.func = dojo.hitch(this,this._startHighlightEffect);
    this.func();
}

我面临的问题是它说,“this._headerDiv is undefined”。在使用 firebug 检查时,this._headerDiv 的范围是 Window 而不是父窗口部件。

请帮助我了解我在这里缺少什么。

最佳答案

@jbabey 描述的内容会起作用,但就 dojo.hitch 而言,您在错误的函数上使用了它。您需要连接传递给 setInterval 的函数。

_startHighlightEffect : function() {
  var blinkInterval = 5000; //Scope here is that of the parent widget

  // hitch the function that will be executed by the setInterval call *********
  window.setInterval (dojo.hitch(this, function() {
          dojo.fadeOut(
          {
                  node: this._headerDiv.domNode,
                  onEnd: dojo.hitch(this, function() {
                          dojo.fadeIn(
                                {node: this._headerDiv.domNode},3000).play();
                  })
          },3000).play();
  }, blinkInterval));
},

_highlightEffect : function() {
  this._startHighlightEffect();
}

关于javascript - window.setInterval() 的 dojo.hitch() 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12265661/

相关文章:

当在函数内的 if 语句的条件中使用全局变量时,Python 抛出 UnboundLocalError

javascript - 函数/对象字面量之前的问号意味着

javascript - React切换监听html的其他节点

javascript - 有人可以解释一下我对这个 Javascript 的范围问题吗?

javascript - 如何在 `HTML label`之前动态添加 `ValidationTextBox`?

javascript - 错误 : defineAlreadyDefined

javascript - jQuery load() 函数内的作用域

javascript - 为什么 var c = "a"|| "b"返回 c = "a"和 var c = "a"&& "b"返回 c = "b"?

javascript - 如何更新 Knockout.js 中的对象属性值

javascript - 自动刷新 dgrid 内容