Search code examples
javascriptjqueryasp.netrefreshpage-refresh

Turn on / off auto-refresh


I have a page that may or may not refresh itself.

And I have a checkbox that will control it, but I have no idea how to implement it.

I've tried to use <asp:Timer> component, but it doesn't work.

Using setInterval / setTimeout from JS, I can't achieve this.

My question is - What .NET component or JS/Jquery attribute I have to use to achieve this?


Solution

  • You can use this if you are using jQuery:

    $(function(){
        
        $('#timercheck').on('change', function () {
           if ($(':checked').length > 0) {
              clearInterval(timer);
           }
        });
    
        var timer = setInterval(function(){
           window.location.reload();
        },5000);
    
    });
    

    Here #timercheck is the id of checkbox.

    Demo @ Fiddle