harsh
harsh

Reputation: 131

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()",1000); 
    }); 
}

Upvotes: 0

Views: 2811

Answers (2)

HyderA
HyderA

Reputation: 21371

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 );
});

Upvotes: 1

Satish
Satish

Reputation: 6485

use preventDefault() after your first click

Upvotes: 0

Related Questions