首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript clearTimeout

JavaScript clearTimeout
EN

Stack Overflow用户
提问于 2013-04-01 09:51:44
回答 1查看 182关注 0票数 0

好的..。如果用户采取了任何行动,我必须清除setTimeout,我不希望它继续并重定向到谷歌。但是它不起作用..有人能看看这个吗?

代码语言:javascript
复制
script type="text/javascript">

function redirect() {
    window.location = "https://google.com";
 }

var timer = window.setTimeout("redirect()", 8000);
var capital = window.prompt("MY Question?","");

if(capital != null && capital.length < 1)  {
clearTimeout(timer);
document.getElementById("firstdiv").innerHTML="Sorry.<br /> The answer is.";
 }
else {
if(window.confirm("Is that your final answer?")){ 

 document.getElementById("firstdiv").innerHTML = "The answer to the question is: " + capital + ", so says you.";}
else{
var capital = window.prompt("What is the capital of Missouri?","");
 document.getElementById("firstdiv").innerHTML = "The answer to the question is: " + capital + ", so says you.";}
</script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-01 09:54:21

您需要将计时器的ID传递给clearTimeout

代码语言:javascript
复制
clearTimeout(timer);

此外,不要将字符串传递给setTimeout。传递一个函数:

代码语言:javascript
复制
var timer = window.setTimeout(redirect, 8000);
// Or
var timer = window.setTimeout(function() {
    redirect();
}, 8000);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15736625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档