Reputation: 171
I want to do a browser redirect using this function:
<script type="text/javascript">
window.onload=function(){
window.setTimeout(function(){
window.location.href = "mypage.html";
}, 20000);
}
</script>
But I want to give user a way to stop the onload when click stop link, below is my code:
(<a href="javascript:void()" style="text-decoration:underline; color:#FFFF00" onclick="window.onload()=null" id="stop"> stop </a>
)
but, onload function still not stopped, I am just a beginner student in javascript, plz help me how to stop onload function when we click stop
Upvotes: 1
Views: 2461
Reputation: 449
To unset the event handler, you have to assign null to window.onload
window.onload = null;
However, this will not fix your problem as you have started a timer at that point. That's what you need to remove. Firstly, store a reference to your timeout in a variable:
var redirectTimeout = window.setTimeout(function() {
window.location.href = "mypage.html";
}, 20000);
And then use clearTimeout to stop the timer:
window.clearTimeout(redirectTimeout);
Upvotes: 6