How do i timeout a jquery script thats set to run every second after half an hour?
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.com/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({
typ开发者_JAVA技巧e: "POST",
url: "ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("ol#updates").prepend(html);
$("#more"+ID).remove();
}
})
}
return false;
}
</script>
<style>
Use 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>
精彩评论