admin 管理员组

文章数量: 1086019

Hy my question really is about how to kill an already onload redirection.

I'v got a between page opening, that's set for an 10 sec delay before going to the destination:

var strUrl = /*from query string, or somewhere else..*/

window.onload = function()
{ 
  window.setTimeout('redirect(strUrl)', 10000);
}
function redirect(strUrl)
{
  document.location=strUrl;
}

Now in the HTML body i'v got some UI elements, 2 buttons (continue, bookmark), 1 checkbox (don't show anymore,..)

Something like:

<body onOrientationChange="smartOrientation();">

  <div id="img_logo">
    <img src="#">  
  </div>

  <div id="txt">
    <p>
      ...blabla...
    </p>   
  </div>

  <div id="btn_bookmark">
    <a href=""  
        class="button green" 
        name="name" 
        type="submit" 
        value="Bookmark">Bookmark
    </a>
  </div>

  <!-- checkboxes,..etc -->

</body>

Now what I WANT is to always have the setTimeout redirection active, but if in those 10 sec. user clicks on the checkbox or the bookmark button, i want it to break/stop.

PSEUDO:

onload: setTimeOut(URL,10sec); - 1,..2,..3,..

listener: if(#div1 || #div2 || ..#divN) are clicked? -> immediately STOP/BREAK setTimeout (kill the 10 seconds bomb)

ps (side Q): I use jquery, and have set onload after dom.ready() because in that whay dom.ready is always a little faster than onload. Is there gonna be a race problem because of this?

Hy my question really is about how to kill an already onload redirection.

I'v got a between page opening, that's set for an 10 sec delay before going to the destination:

var strUrl = /*from query string, or somewhere else..*/

window.onload = function()
{ 
  window.setTimeout('redirect(strUrl)', 10000);
}
function redirect(strUrl)
{
  document.location=strUrl;
}

Now in the HTML body i'v got some UI elements, 2 buttons (continue, bookmark), 1 checkbox (don't show anymore,..)

Something like:

<body onOrientationChange="smartOrientation();">

  <div id="img_logo">
    <img src="#">  
  </div>

  <div id="txt">
    <p>
      ...blabla...
    </p>   
  </div>

  <div id="btn_bookmark">
    <a href=""  
        class="button green" 
        name="name" 
        type="submit" 
        value="Bookmark">Bookmark
    </a>
  </div>

  <!-- checkboxes,..etc -->

</body>

Now what I WANT is to always have the setTimeout redirection active, but if in those 10 sec. user clicks on the checkbox or the bookmark button, i want it to break/stop.

PSEUDO:

onload: setTimeOut(URL,10sec); - 1,..2,..3,..

listener: if(#div1 || #div2 || ..#divN) are clicked? -> immediately STOP/BREAK setTimeout (kill the 10 seconds bomb)

ps (side Q): I use jquery, and have set onload after dom.ready() because in that whay dom.ready is always a little faster than onload. Is there gonna be a race problem because of this?

Share Improve this question asked Jan 26, 2011 at 9:28 PathOfNeoPathOfNeo 1,0894 gold badges21 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

declare the timeout in a variable:

var t = window.setTimeout('redirect(strUrl)', 10000);

and later use that variable

clearTimeout(t);

to kill it

more here

besides that: use document.ready() OR onload , to prevent yourself from frenzy-racing conditions.

 var mytimer = window.setTimeout('redirect(strUrl)', 10000);

 /*....*/

 if (condition){

   clearTimeout(mytimer)

 }

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)