jquery- stop auto refreshing onClick
I'm running a jquery autorefresh code which get active soon the page is loaded. I've set timeinterval to 1second.which means that for every 1 sec page gets refreshed. I want to know is there any method to stop auto refreshing onclick?
Code:
$(document).ready(function(){
setTimeout("dummy()",500);
});
function dummy(){
setTimeout("checkalerts()",50);
}
function checkalerts() {
var url="includes/adda/hangouts/display_all_hangouts.php";
$.post(url,function(data){
$("#print").html(data).show();
setTimeout("dummy()",100开发者_运维知识库0);
});
}
After further reviewing your code, I'm a little baffled. I don't understand why you need so many timers. You essentially have one timer that calls dummy() every half a second, and that function creates a new timer (every half a second, a new timer is created) every 50 millisecond.
And when posting you're calling dummy again every second. This is some serious WTF code!
Anyway, updated code, this snippet should work
var timer;
$( document ).ready(function(){
timer = setTimeout( "checkalerts()", 1000);
});
function checkalerts()
{
var url="includes/adda/hangouts/display_all_hangouts.php";
$.post( url, function( data )
{
$("#print").html(data).show();
});
}
$( '#my-button' ).click( function()
{
clearTimeout( timer );
});
use preventDefault() after your first click
精彩评论