在jquery绑定事件上的RequireJS不会触发方法
问题描述:
在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再现错误
答
将字符串传递给setTimeout
既不好也不必要,并且做因此导致您遇到的问题,因为字符串的评估被延迟,因此发生在showRowItem
可用的上下文之外。你可以这样做,而不是:
window.setTimeout(showRowItem.bind(globalMouseOverItem), inDelay);
或者与当前的代码,你有这相当于:
window.setTimeout(showRowItem.bind(this), inDelay);
,你可以省略globalMouseOverItem
。
非常感谢,就是这样! – deroccha