好的..如果用户采取任何行动,我必须清除setTimeout,我不希望它继续并重定向到谷歌。但它不起作用..有人可以看一下吗?
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>
答案 0 :(得分:0)
您需要将计时器的ID传递给clearTimeout
:
clearTimeout(timer);
另外,请勿将字符串传递给setTimeout
。传递函数:
var timer = window.setTimeout(redirect, 8000);
// Or
var timer = window.setTimeout(function() {
redirect();
}, 8000);