javascript - 如何知道每 3 分钟的窗口 href 值

标签 javascript

当我转到下一个 URL 时,警报不起作用。我需要在窗口位置 onchange 上每 3 分钟获取一次窗口 href 值。

var i=0;
function load(){

  var currentLoc = window.location.href;
  PrevLoc = currentLoc;
  interval(currentLoc,PrevLoc);
}

function interval(currentLoc,PrevLoc){
  setInterval(function () {

    if(currentLoc == 'https://github.com/features'){
 		  alert('Reached the Target Page')
    //  document.write('Reached the Target Page');
      return true;
    }

    if(currentLoc == PrevLoc){
      	alert(window.location.href);
    //  document.write(window.location.href);

      if(i==0){
        PrevLoc = window.location.href;
        window.location.href = "https://github.com/join?source=login";
        currentLoc=window.location.href;
      }
      interval(currentLoc,PrevLoc);
      i++;

    }
    else {
    	alert(window.location.href);
 //     document.write(window.location.href);
    }
  },3000);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">   </script>
<body onLoad="load();">
</body>

我正在使用 setInterval 使用 javascript 和 jquery 尝试此操作。但当更改 href 值时,它不起作用。

 <html>
 <head>


 </head>
 <body onLoad="load();">

 <script>
  function load(){
    window.location.href = "https://github.com/join?source=login"
    var PrevLoc = "https://github.com/join?source=login";
    var currentLoc = window.location.href;

    if(currentLoc == PrevLoc){
       alert('inside')
       setInterval(function () {
         alert('changed')
      },3000);
    }
   else {

   }
 }
</script>
</body>
</html>

最佳答案

更改此:

if(currentLoc == PrevLoc){
   alert('inside')
   setInterval(function () {
     alert('changed')
  },3000);
}

对此或您想要检查的任何内容:

setInterval(function () {
  if(currentLoc != PrevLoc){
     alert('changed')
  }
},3000);

关于javascript - 如何知道每 3 分钟的窗口 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44822673/

相关文章:

javascript - javascript中的()用于初始化变量有什么用?

javascript - 单击 Angular 8 滚动到页面顶部

javascript - 为什么 Microsoft Edge 中会出现关闭警告?

javascript - 找到下一个 div-jquery 中存在的下一个元素的 id

javascript - 如何将 'svg'格式的图标添加到SweetAlert for Bootstrap?

javascript - 如何在不将文本包装在任何标记中的情况下将文本对齐到 div 的底部?

javascript - 将图像放入 chrome.storage 扩展中

javascript - 用整数替换字母并用它放置 "-"(破折号)

javascript - 我不明白为什么点击链接时似乎返回 false - 控制台有帮助吗?

javascript - 使用重力效果拖动元素