5秒后显示JS Alert

时间:2016-02-22 03:51:18

标签: javascript alert countdown

我试图在5秒后显示JS警报。现在我有这个代码:

<script>
function init() {
    var count=5;
    var counter=setInterval(timer,1000);
        function timer(){
            count=count-1;
            if(count==0){
                alert("This is an alert")
                window.location = "http://www.example.com";      
                return;
            } 
        }
    }
    window.onload = init;
</script>

问题在于它无法正常工作。我无法在代码中看到某种小错误。

5 个答案:

答案 0 :(得分:2)

为什么你使用setInterval并保持计数变量在五秒钟后推断?

您可以使用setTimeout简化代码。例如:

window.onload = setTimeout(function(){
    alert('This is an alert');
    window.location = 'http://www.example.com';
}, 5000);

答案 1 :(得分:0)

您的代码运行正常。请查看JSFIDDLE

  

5秒后显示JS提醒

当您使用setInterval时,timer函数将每1秒执行一次,即使在发出警报后也会继续执行。 从开发人员的工具中检查控制台

不确定您是否需要setInterval或者您确实在寻找setTimeout

答案 2 :(得分:0)

尝试

&#13;
&#13;
// ---------------- Try
function init() {
    var count = 5; // set secconds
    var counter = setInterval(timer, 1000 * count);

    function timer() {
        alert("This is an alert")
        window.location = "http://www.example.com";
        //clearInterval(counter) // stop interval
    }

}

window.onload = init;

// --------------- Or

function init() {
    var count = 5; // set secconds
    var counter = setInterval(function() {
        alert("This is an alert")
        window.location = "http://www.example.com";
        //clearInterval(counter) // stop interval
    }, 1000 * count);
}

//window.onload = init;



// --------------- Or

function init() {
    var count = 5; // set secconds
    var counter = setTimeout(function() {
        alert("This is an alert")
        window.location = "http://www.example.com";
    }, 1000 * count);
}

//window.onload = init;
&#13;
&#13;
&#13;

答案 3 :(得分:0)

你可以用简单的方法

$(document).ready({

setInterval(showAlert(), 5000);
});

function showAlert() {
alert("This is an alert");
window.location = "http://www.example.com";      
return;
enter code here

注意:1秒等于1000,因此您希望此功能在5秒后运行,您需要设置5000。

答案 4 :(得分:0)

您的代码运行正常。只是由于某种原因没有在窗口加载时调用你的init函数。尝试直接调用您的函数或使用document.body.onload或在HTML文件的末尾插入脚本标记:

&#13;
&#13;
//I have a thing of cleaning up code
function init() {
  var count = 5;
  var counter = setInterval(timer, 1000);
  function timer(){
    count -= 1; //count = count-1 and count -= 1 are the same
    if (count === 0) { //use triple equals sign
      clearInterval(counter); //delete the interval when we are done with it
      alert("This is an alert"); //semi-colon
      window.location = "http://www.example.com";
      return;
    }
  }
}
//Instead of releying on a event, we can just call the function directly
init();
//Or you can check out the HTML for another way
&#13;
<!DOCTYPE html>
<html>
  <body>
    <!--
    All you content will go here.
    And then the script will be placed right after it. It will be ran when the whole document is loaded.
    You can do something like this -->
    <script>/*function init() {...*/</script>
  </body>
</html>
&#13;
&#13;
&#13;