I have a jQuery script that runs a function every second with setinterval. But i want to set a timeout for that so after half an hour it kills the script. How do i do this? Thanks :)
source code:
<script type="text/javascript" src="/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
setInterval(oneSecondFunction, 1000);
});
function oneSecondFunction() {
//var ID = $(this).attr("id");
var ID = '1';
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
})
}
return false;
}
</script>
<style>
I have a jQuery script that runs a function every second with setinterval. But i want to set a timeout for that so after half an hour it kills the script. How do i do this? Thanks :)
source code:
<script type="text/javascript" src="http://ajax.googleapis./ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
setInterval(oneSecondFunction, 1000);
});
function oneSecondFunction() {
//var ID = $(this).attr("id");
var ID = '1';
if(ID){
$("#more"+ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
})
}
return false;
}
</script>
<style>
Share
Improve this question
asked Apr 14, 2011 at 20:28
user663049user663049
1,1783 gold badges14 silver badges28 bronze badges
0
3 Answers
Reset to default 5Use this pattern:
var timer = null;
$(function() {
timer = setInterval(oneSecondFunction, 1000);
setTimeout(function() { clearInterval(timer); }, 30*60*1000);
});
Save the timer it creates when you do setInvterval
and clear the interval after 30 minutes.
I.E.
var t = setInterval(oneSecondFunction, 1000);
setTimeout(function() { clearInterval(t); }, 30*60*1000);
you can do this:
<script type="text/javascript">
$(function() {
var interval= setInterval(oneSecondFunction, 1000);
setTimeout(function(){clearInterval(interval)}, 18000000); //half an hour
});
...
</script>
<style>