2016-06-08 39 views
0

在jQuery中使用requirejs我面臨綁定事件調用方法的問題。具有下列代碼:在jquery綁定事件上的RequireJS不會觸發方法

define(["jquery"], function($) { 
 

 

 
    function showRowItem(item) { 
 
     console.log('method in'); 
 
     
 
    } 
 
    
 
    jQuery(document).ready(function() { 
 

 
     jQuery('ul#topnav-firstrow li').each(function() { 
 

 
      jQuery(this).bind("mouseover", function (event) { 
 
       if (outTimerID != null) { 
 
        clearTimeout(outTimerID); 
 
        outTimerID = null; 
 
       } 
 
       globalMouseOverItem = this; 
 
       inTimerID = window.setTimeout("showRowItem(globalMouseOverItem)", inDelay); 
 
      }); 
 
      
 
      }); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

當我將鼠標懸停在項事件被炒魷魚,但方法不承認

Uncaught ReferenceError: showRowItem is not defined 

這裏是codepen http://codepen.io/deroccha/pen/WxQLoy再現錯誤

回答

0

將字符串傳遞給setTimeout既不好也不必要,並且做因此導致您遇到的問題,因爲字符串的評估被延遲,因此發生在showRowItem可用的上下文之外。你可以這樣做,而不是:

window.setTimeout(showRowItem.bind(globalMouseOverItem), inDelay); 

或者與當前的代碼,你有這相當於:

window.setTimeout(showRowItem.bind(this), inDelay); 

,你可以省略globalMouseOverItem

+0

非常感謝,就是這樣! – deroccha