执行此代码时,我看不到警告框

I cant see the alert box while executing this code

本文关键字:看不到 警告 我看 代码 执行      更新时间:2023-09-26

在这个代码上,当我运行这个代码时,我看不到我的警报框

setInterval(function () {
    location.reload(true);
    $(document).ready(function(){
        alert($("h2").text())
    }) 
}, 6000)

页面在6秒后重新加载,但我没有看到我的提醒框。我想知道为什么,假设标签h2包含一个文本。。。感谢

一旦执行location.reload(true),页面就会重新加载,脚本的其余部分永远不会执行。

如果你想在页面重新加载之前发出警报(烦人的演示#1),请这样做:

$(document).ready(function () {
    setInterval(function () {
        alert($("h2").text());
        location.reload(true);
    }, 6000);
});

如果你想在页面加载后(即第一次加载和每次重新加载后)发出警报(烦人的演示#2),请这样做:

$(document).ready(function () {
    alert($("h2").text());
    setInterval(function () {
        location.reload(true);
    }, 6000);
});

You'r事件在执行之前已经发生。应该用这样的东西代替。。。

setInterval(function(){
  (function(){
    alert($("h2").text());
    location.reload(true);       
  })();
}, 6000);