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 badges2 Answers
Reset to default 6declare 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)
}