我一直在使用時間事件時遇到問題。有人能解釋爲什麼A不行,B行嗎?唯一的區別在於我把事件綁定在一個函數中。不要擔心功能關閉,它與問題無關。當我測試A時,沒有js錯誤,但計時器未被清除。JavaScript時間事件問題(setTimeout/clearTimeout)
A - >
Test.Navigation = (function() {
var openTimer = null;
var closeTimer = null;
var addListeners = function() {
$('.hover_container').on('mousemove', function(e) {
clearTimeout(closeTimer);
});
$('.hover_container').on('mouseleave', function(e) {
// set the close timer
var container = this;
closeTimer = setTimeout(function() {
//has the mouse paused
close(container);
}, 750);
});
};
return {
init : function() {
addListeners();
}
};
})();
乙 - >
Test.Navigation = (function() {
var openTimer = null;
var closeTimer = null;
$('.hover_container').on('mousemove', function(e) {
clearTimeout(closeTimer);
});
$('.hover_container').on('mouseleave', function(e) {
// set the close timer
var container = this;
closeTimer = setTimeout(function() {
//has the mouse paused
close(container);
}, 750);
});
var addListeners = function() {
// nothing here
};
return {
init : function() {
addListeners();
}
};
})();
編輯:請忽略容器部分,它沒有任何道瓊斯指數第i個問題簡直是完整的代碼的一部分,我沒有拿出
使用'setTimeout'單個時間實例。使用'setInterval'來保持持續時間。 –
聲明'變種容器=此;'在該容器的直接範圍,而不是在另一功能。在** A **中,容器不是你所需要的。 –
@dystroy容器正是我需要的,這就是問題的旁邊,如果清除定時器工作的關閉功能將不火 – Huangism