admin管理员组文章数量:1134081
I want to stop this interval in the error
handler from running repeatedly. Is that possible, and if so, how?
// example code
$(document).on('ready',function(){
setInterval(updateDiv,3000);
});
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
// I want to stop it here
}
});
}
I want to stop this interval in the error
handler from running repeatedly. Is that possible, and if so, how?
// example code
$(document).on('ready',function(){
setInterval(updateDiv,3000);
});
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
// I want to stop it here
}
});
}
Share
Improve this question
edited Oct 17, 2018 at 12:16
Henrik Petterson
asked May 8, 2013 at 9:31
Henrik PettersonHenrik Petterson
7,09421 gold badges79 silver badges157 bronze badges
1
- 2 Possible duplicate of Stop setInterval call in JavaScript – Mohammad Commented Oct 17, 2018 at 10:57
6 Answers
Reset to default 270You need to set the return value of setInterval
to a variable within the scope of the click handler, then use clearInterval()
like this:
var interval = null;
$(document).on('ready',function(){
interval = setInterval(updateDiv,3000);
});
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
clearInterval(interval); // stop the interval
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
}
});
}
Use a variable and call clearInterval
to stop it.
var interval;
$(document).on('ready',function()
interval = setInterval(updateDiv,3000);
});
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
// I want to stop it here
clearInterval(interval);
}
});
}
You have to assign the returned value of the setInterval
function to a variable
var interval;
$(document).on('ready',function(){
interval = setInterval(updateDiv,3000);
});
and then use clearInterval(interval)
to clear it again.
USE this i hope help you
var interval;
function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
/* clearInterval(interval); */
stopinterval(); // stop the interval
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
}
});
}
function playinterval(){
updateDiv();
interval = setInterval(function(){updateDiv();},3000);
return false;
}
function stopinterval(){
clearInterval(interval);
return false;
}
$(document)
.on('ready',playinterval)
.on({click:playinterval},"#playinterval")
.on({click:stopinterval},"#stopinterval");
we can easily stop the set interval by calling clear interval
var count = 0 , i = 5;
var vary = function intervalFunc() {
count++;
console.log(count);
console.log('hello boy');
if (count == 10) {
clearInterval(this);
}
}
setInterval(vary, 1500);
var flasher_icon = function (obj) {
var classToToggle = obj.classToToggle;
var elem = obj.targetElem;
var oneTime = obj.speed;
var halfFlash = oneTime / 2;
var totalTime = obj.flashingTimes * oneTime;
var interval = setInterval(function(){
elem.addClass(classToToggle);
setTimeout(function() {
elem.removeClass(classToToggle);
}, halfFlash);
}, oneTime);
setTimeout(function() {
clearInterval(interval);
}, totalTime);
};
flasher_icon({
targetElem: $('#icon-step-1-v1'),
flashingTimes: 3,
classToToggle: 'flasher_icon',
speed: 500
});
.steps-icon{
background: #d8d8d8;
color: #000;
font-size: 55px;
padding: 15px;
border-radius: 50%;
margin: 5px;
cursor: pointer;
}
.flasher_icon{
color: #fff;
background: #820000 !important;
padding-bottom: 15px !important;
padding-top: 15px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<i class="steps-icon material-icons active" id="icon-step-1-v1" title="" data-toggle="tooltip" data-placement="bottom" data-original-title="Origin Airport">alarm</i>
本文标签: javascriptStop setIntervalStack Overflow
版权声明:本文标题:javascript - Stop setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736796824a1953318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论