0

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

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>
4

1 回答 1

0

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

clearTimeout(timer);

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

var timer = window.setTimeout(redirect, 8000);
// Or
var timer = window.setTimeout(function() {
    redirect();
}, 8000);
于 2013-04-01T01:54:20.913 回答