admin管理员组

文章数量:1320837

I was trying to execute a javascript 10 seconds after a particular click event.Hence i used setTimeout

setTimeout(function () {
    alert('hello');
}, 10000);

It works if i'm on the same page after the click.But if i navigate to some other page it doesn't work. So is it right to assume that setTimeout works with respect to a particular page and not the whole application.

Is there any way to do what i'm trying to achieve.

I was trying to execute a javascript 10 seconds after a particular click event.Hence i used setTimeout

setTimeout(function () {
    alert('hello');
}, 10000);

It works if i'm on the same page after the click.But if i navigate to some other page it doesn't work. So is it right to assume that setTimeout works with respect to a particular page and not the whole application.

Is there any way to do what i'm trying to achieve.

Share Improve this question asked Oct 17, 2013 at 8:09 iJadeiJade 23.8k58 gold badges160 silver badges250 bronze badges 1
  • js timers works in the page scope, and yes, when you leave the page all timers will vanish as well. you can set the timeout/interval on each page, OR set it once on a page with a fullscreen iframe and change the pages within the iframe (but keep the 'father' page active all the time) – geevee Commented Oct 17, 2013 at 8:12
Add a ment  | 

5 Answers 5

Reset to default 8

Everything that happens in the browser is document bound. When you navigate away from the current document (page) it bees obsoleted including timers - as expected.

So what you try here is not possible just by using timers.

You can write a localStorage (or sessionStorage) item with time the timer started and when it should finish, and from the new page (provided it is from the same origin) you read the item and calculate the difference and start a new timer (or add the parameters as part of the url you navigate to).

Javascript code is page bound.
If you have included x1.js on page1, then this script won't execute on page2 unless you have included that or put in global scope.
When you navigate to another page and if browser is refreshed, loaded scripts will be lost and new page with included scripts will be loaded.

Yes, it is with respect to the page it's on. You could add a flag that says whether the timeout has

  • not been set
  • been set but not run or
  • set and already run.

Then, if the user navigates to a different page, you could check that flag in the onunload event. If it's been set and not called yet, then simply call it.

When you navigate to another page,the browser will load the new page and all your setTimeout/etc in your old page will lost,just like game over and start a new game.

You may try Single Page App which all your page is in the same DOM and can achieve your result

Try this solution:

In your firstpage.html,

function initFunc() {
    setTimeout(function () {
       alert('hello');
    }, 10000);
}
$(document).ready(initFunc);

now in secondpage.html you are able to call initFunc() whenever you need to.

If you need to call a function in first page to second, you should write the function as global

本文标签: javascriptsetTimeout doesn39t work if i navigate to another pageStack Overflow