ratherBeKiting ratherBeKiting - 11 months ago 110
Javascript Question

Prevent Javascript Page Reload

I am using this javascript to auto refresh a page every 30 seconds.

<script type="text/javascript">
window.setTimeout(function(){ document.location.reload(true); }, 30000);

I need to give users an option to disable this function at any time before the page is reloaded.

Note: this is for an admin panel, not a public website, and the requirement from the client is that the page auto refreshes, with an option to stop the refresh before it happens by clicking a button.

Is there any way to achieve this... That is.. disable the javascript before it executes?

Answer Source

clearTimeout() : Cancels a timeout previously established by calling setTimeout().

You're searching for clearTimeout() :

var refresh = window.setTimeout(function(){ document.location.reload(true); }, 30000);

$('body').on('click', '#my-button', function(){

Hope this helps.