TyForHelpDude TyForHelpDude - 1 year ago 85
jQuery Question

how to run javascript function after location.reload() in same browser window

I have simple function;

setTimeout(function(){ alert('hello');
//Business stuffs..

after open in a browser(chrome or firefox) I type this;

//Business stuffs...

After that once I type;


I want this function executes in same browser window forever.. or till close the browser..
but as you guess, it only works once.
How can I achieve this ?

Answer Source

You should create the timer on page load, and reload the page in the callback. When the page reloads, the timer will be re-created. This will "recursively" reload the page every 5 seconds.

    <script type="text/javascript">
      console.log('page loaded, waiting...');
      setTimeout(function () {
      }, 5000);

As far as I know, there is no way to achieve this if you don't control the page you want to reload (i.e., using Dev Tools or console APIs). It's impossible to attach a script to execute on the next page load, and any active timers are cleared when the current page unloads. A browser extension is probably the way to go; for example, Content Scripts can be used to achieve this in Chrome.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download