简体   繁体   English

setTimeout放在标头中,我如何限制运行1次

[英]setTimeout placed in header , how can i restrict to run 1 time

How can i make sure this doesn't execute again. 我如何确保这不会再次执行。 I am redirecting all my pages to a single page on my site. 我将所有页面重定向到网站上的单个页面。 It keeps refreshing/reloading that page because the script is placed in the header and must be. 因为脚本位于标头中并且必须存在,所以它会刷新/重新加载该页面。

   setTimeout(function(){
     window.location="mysite/page";
   },1000);

You can add an hash to your url when you refresh the page: 您可以在刷新页面时在网址中添加哈希值:

<html>
<head>
<script type="text/javascript">
    var loc = window.location.href;
    if (loc.indexOf ("#DoNotRefresh") < 0) {
        setTimeout(function(){
            window.location="#DoNotRefresh";
        },1000);
    }
    console.log(new Date());
</script>
</head>

Cant you just check to see if the location is already set? 您不能检查位置是否已设置?

  if(window.location !== 'mysite/page')
     {
        setTimeout(function()
        {
         window.location = 'mysite/page';
        }, 1000); 
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM