最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - KILL THE timed delay function "setTimeout redirecting to page" IF SOME UI elements are clicked -

programmeradmin5浏览0评论

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)

 }
发布评论

评论列表(0)

  1. 暂无评论