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

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

本文关键字:何限制 运行 setTimeout      更新时间:2023-09-26

我怎样才能确保它不会再次执行。我正在将所有页面重定向到我网站上的一个页面。它会不断刷新/重新加载该页面,因为脚本放置在标题中并且必须放在标题中。

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

您可以在刷新页面时向网址添加哈希:

<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>

您不能检查一下位置是否已设置吗?

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