Javascript 当鼠标在div上时jQuery停止超时
我有课:Javascript 当鼠标在div上时jQuery停止超时,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有课: <ul class="topStatsWrapper"> <li><a href="">link</a></li> <li><a href="">link</a></li> <li><a href="">link</a></li> </ul> 完整代码不太确定我们是否有足够的信息来传播正确的答案
<ul class="topStatsWrapper">
<li><a href="">link</a></li>
<li><a href="">link</a></li>
<li><a href="">link</a></li>
</ul>
完整代码不太确定我们是否有足够的信息来传播正确的答案。首先,我将把你的setTimeout声明为一个变量,我可以用鼠标在你的类上清除它
var timer; // set this globally
// this is from within your function
initialize: function() {
timer = setTimeout( _.bind( this.updateStats, this, true ), 3 * 1000);
},
然后使用一个事件
$(document).on("mouseover", ".myclass", function(){
timer.clearTimeout();
});
$(document).on("mouseover", ".myclass", function(){
timer.clearInterval();
});
因此,请改用setInterval:
var timer; // set this globally
// this is from within your function
initialize: function() {
timer = setInterval( function(){ ... your code ... }, 3000);
},
然后使用一个事件
$(document).on("mouseover", ".myclass", function(){
timer.clearTimeout();
});
$(document).on("mouseover", ".myclass", function(){
timer.clearInterval();
});
将您的
updateStats
函数更改为此
updateStats: function() {
if (window._noUpdate === true) return;
var that = this, update;
this.showLoading( true );
update = $.when( update,
that.ajax( 'getTopics', null, '.newestThreads' ),
that.ajax( 'getPosts', {limit: JSON.stringify([0, 3])}, '.newestPosts' )
);
update.done( function() {
that.showLoading();
window.setTimeout( _.bind( that.updateStats, that, true), that.interval * 1000);
});
}
initialize: function() {
this.interval = parseInt($('.topStatsWrapper').attr('data-topstats-interval'));
window.setTimeout( _.bind( this.updateStats, this, true ), 3 * 1000);
$("body").on("hover", ".topStatsWrapper",
function() {
window._noUpdate = true;
},
function() {
window._noUpdate = false;
}
});
},
并将初始化
函数更改为此
updateStats: function() {
if (window._noUpdate === true) return;
var that = this, update;
this.showLoading( true );
update = $.when( update,
that.ajax( 'getTopics', null, '.newestThreads' ),
that.ajax( 'getPosts', {limit: JSON.stringify([0, 3])}, '.newestPosts' )
);
update.done( function() {
that.showLoading();
window.setTimeout( _.bind( that.updateStats, that, true), that.interval * 1000);
});
}
initialize: function() {
this.interval = parseInt($('.topStatsWrapper').attr('data-topstats-interval'));
window.setTimeout( _.bind( this.updateStats, this, true ), 3 * 1000);
$("body").on("hover", ".topStatsWrapper",
function() {
window._noUpdate = true;
},
function() {
window._noUpdate = false;
}
});
},
这将设置一个全局变量(
\u noUpdate
),表示光标是否悬停在ul上。如果将其设置为true
,则更新功能将简单地退出,而不做任何操作。在您的情况下\uuu
有什么作用?@motanelu IDK,我正在开发IPS4 hook。也许你能在那里找到什么<代码>;(函数($,,未定义){“使用严格”;当您关闭iLife时,最后是什么?函数updateStats()
也必须创建一个超时,否则它只会更新一次然后停止。如果看不到也没有办法回答。@Archer full code这不是一个间隔,而是一个超时。只要this.updateStats
创建另一个超时(它必须这样做,否则它只会运行一次)你的全局变量变得不相关了。嗯。我如何启动这个超时?阿彻:是的,我有两个超时。一个是这个。updateStats
第二个是那个。updateStats
是的,但我想他想要一种停止超时的方法。全局变量每3秒被覆盖一次,所以如果他想停止计时器2秒,他会这样做如果你想启动超时,我会把它改为setInterval。它现在不更新未捕获的引用错误:$document is not definedIt应该是$(document)
。确保已正确复制并粘贴了上述代码。这样错误消息现在就消失了。您现在是否有任何错误?未捕获引用错误:$document未定义…我认为它是IPS4 javascript,但与jQuery类似,没有$document