MM0959
MM0959

Reputation: 167

stopping page reload when button is clicked

I am trying to reload a page with a button in it. When the user click on the button an alert window should popup and the page reload should stop until ok is clicked on the alert window.

I am writing my code as follows:

<button id="myButton" onclick="myfunction()"> Test </button>

<script type="text/javascript">

 function myfunction()
  {
    if ( document.getElementById("myButton").onclick == false )
     {
        window.setTimeout(function(){ document.location.reload(true); }, 1000);
     }
    else
    {
       alert ( "Timer Not Set." );
    }
  }
</script>

Neither the if or else conditions are working.

Upvotes: 0

Views: 1063

Answers (1)

Abhitalks
Abhitalks

Reputation: 28387

Use setInterval and then clearInterval whenever you want to pause or stop, and then reassign setInterval to resume.

Something on the lines of this:

var timer = window.setInterval(reloader, 3000);

$("#btn").on("click", function() {
    clearInterval(timer);
    alert("Just click ok");
    timer = window.setInterval(reloader, 3000);
});

function reloader() {
    document.location.reload(true);
}

Demo: http://jsfiddle.net/abhitalks/79v0xfjd/1/

.

Upvotes: 1

Related Questions