2012-09-04 17 views
3

我試圖用dojo fadeIn/Out產生閃爍的效果。dojo.hitch()作用域爲window.setInterval()

下面的代碼片段是一個小部件類的聲明中定義:

_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是不確定的」。在檢查螢火蟲時,this._headerDiv的範圍是Window而不是父窗口小部件。

請幫我理解我在這裏錯過了什麼。

回答

2

你可以保存上下文時,它就是你想要的背景下,並在以後使用它:

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

什麼@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(); 
} 
+0

我覺得你需要在'dojo.hitch'中包裝'onEnd'回調函數。 – Lucas

+0

你是對的。謝謝。 –

+0

對不起。此解決方案不適合我。 – Divij