javascript 5秒后显示JS警报

txu3uszq  于 2023-02-21  发布在  Java
关注(0)|答案(6)|浏览(185)

我试图在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>

问题是它不能正常工作。有一些我在代码中看不到的小错误。

xbp102n0

xbp102n01#

为什么要使用setInterval并维护一个count变量来推断五秒的时间?
使用setTimeout可以简化代码。例如:

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

aij0ehis2#

您的代码工作正常。请检查此JSFIDDLE
5秒后显示JS警报
其中,当您使用setInterval时,这意味着timer函数将每1秒执行一次,并且即使在发出警报后也会继续执行。
不确定您是否需要setInterval,或者您实际上正在寻找setTimeout

cig3rfwq

cig3rfwq3#

∮尝试∮

// ---------------- 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;
j9per5c4

j9per5c44#

你可以用的简单方法是

$(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。

nkcskrwz

nkcskrwz5#

你的代码运行良好。只是由于某种原因,你的init函数在窗口加载时没有被调用。尝试直接调用你的函数,或者使用document.body.onload,或者在HTML文件的末尾插入script标签:

//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
<!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>
pgvzfuti

pgvzfuti6#

尝试在setTimeOut中使用function

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

相关问题