我用setInterval(fname, 10000);JavaScript在JavaScript中每10秒调用一次函数.是否有可能在某些事件上停止调用它?
我希望用户能够停止重复刷新数据.
我有一个简单的幻灯片,我在客户的主页上制作,使用setInterval来计算旋转时间.
为防止浏览器在页面未对焦(正在查看另一个选项卡或其他程序)时搞砸setInterval,我正在使用:
function onBlur() {
clearInterval(play);
};
function onFocus() {
mySlideRotateFunction();
};
if (/*@cc_on!@*/false) {
document.onfocusin = onFocus;
document.onfocusout = onBlur;
} else {
window.onfocus = onFocus;
window.onblur = onBlur;
}
Run Code Online (Sandbox Code Playgroud)
mySlideRotateFunction设置setInterval并运行一些jQuery.虽然这在大多数情况下是有效的,但我发现,有时看起来似乎onBlur还没有运行,当我回到页面时,时间已经"积累"并且旋转变得疯狂.
我无法确定为什么有时会发生这种情况的原因,而不是其他原因.
我的问题 - 我的代码存在问题,当浏览器窗口失焦时,是否有人有更好的建议'暂停'setInterval?
谢谢
我正在研究一个计时器,经过我在这个论坛得到的一些答案后,它一直在顺利进行.这就是它现在的样子:(只是为了让你明白这一点)
我的代码(请注意,这是一项正在进行中的工作,因此有些东西还没有完成;基本上每个具有a的函数alert("workin");仍然没有被使用.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>WIP</title>
<meta charset="UFT-8">
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script>
$(document).ready(function() {
timerSet(9,12);
timerRun();
});
function timerReset() {
alert("workin");
}
function timerSet(inputMinutes, inputSeconds) {
minutes = inputMinutes;
seconds = inputSeconds;
finalTimeInSeconds = minutes*60 + seconds; //finalTimeInSeconds is the time it takes for the timer to be 00:00 in seconds.
timerPrint();
}
function timerAdd(inputMinutes, inputSeconds) {
alert("workin");
}
function timerSubtract(inputMinutes, inputSeconds) {
setTimeout(function () {
if(minutes > 0 && seconds == 0) …Run Code Online (Sandbox Code Playgroud)假设我有一些文件:server.php,client.php和一个表:user [id,name,status]
server.php将处理如下事情:更新用户状态等...
在client.php我有一个AJAX调用server.php为如果状态被激活(以每10秒)检查:
$(document).ready(function(){
setInterval(function(){
check_user_status(user_id)
},10000);
});
check_user_status = function (user_id) {
$.ajax({
type: "post",
data: "user_id="+user_id,
url : "server.php",
success: function(data){
// do something with the response
// I want to exit the checking operation if user is already activated
},
error: function(e){
alert("Error occurred");
}
});
}
Run Code Online (Sandbox Code Playgroud)
怎么可能呢?
谢谢你的时间!
可能重复:
如何停止"setInterval"
如何停止跟随setInterval函数进入else条件
setInterval(function () {
if ($('#iframe1').is(':visible')) {
} else {
/// Here i want clear setInterval
}
}, 200);
Run Code Online (Sandbox Code Playgroud) 我喜欢......
<div id="div"></div>
<script src="jquery.js"></script>
<script src="updater.js"></script>
Run Code Online (Sandbox Code Playgroud)
updater我想从我的.php文件中获取数据,所以可能就像...
setInterval(function() {
$.ajax({
type: "GET",
url: "test.php",
success: function(html) {
// html is a string of all output of the server script.
$("#div").html(html);
}
});
}, 5000);
Run Code Online (Sandbox Code Playgroud)
然后,显然足够了test.php的页面.
但是,我想停止setInterval(); 当有人点击评论按钮(textarea)时.我知道,这里有一些; 但他们没有工作?