如何在 JavaScript 中 5 秒后仅刷新页面一次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21245803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:27:33  来源:igfitidea点击:

How to refresh the page only one time after 5 sec in JavaScript?

javascript

提问by Jin Yong

Does anyone know how to refresh the page only one time after 5 sec in JavaScript?

有谁知道如何在 JavaScript 中在 5 秒后仅刷新一次页面?

I have the following script to refresh the page after 5 sec:

我有以下脚本可以在 5 秒后刷新页面:

setTimeout(function () {
    window.location.reload(1);
}, 5000);  // After 5 secs

But somehow this is refresh every 5 sec instead of just once time after the 1st 5 sec. Does anyone know how to fix this issue?

但不知何故,这是每 5 秒刷新一次,而不是在第一个 5 秒后刷新一次。有谁知道如何解决这个问题?

回答by PiotrT

I think better solution is check document.refererand compare with document.location

我认为更好的解决方案是检查document.referer并比较document.location

if (document.referrer !== document.location.href) {
    setTimeout(function() {
        document.location.reload()
  }, 5000);
}

You can try this in jsfiddle: https://jsfiddle.net/ga0Lrurj/

你可以在 jsfiddle 中试试这个:https://jsfiddle.net/ga0Lrurj/

回答by Ethan

This is by far the simplest and lightest solution:

这是迄今为止最简单、最轻便的解决方案:

setTimeout(function () {
    if(window.location.hash != '#r') {
        window.location.hash = 'r';
        window.location.reload(1);
    }
}, 5000);  // After 5 secs

回答by OptimusCrime

You need a way to identify a reloaded page. The easiest way (imo) would be to controll this server side. Check a get-variable and include the js-code if it does not exist.

您需要一种方法来识别重新加载的页面。最简单的方法(imo)是控制这个服务器端。检查获取变量并包含 js 代码(如果它不存在)。

Also, I see people suggesting cookies. I don't like using cookies for minor stuff like this.

另外,我看到有人建议使用 cookie。我不喜欢将 cookie 用于像这样的小事。

You could try something like:

你可以尝试这样的事情:

// Get all get-variables
var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {queryDict[item.split("=")[0]] = item.split("=")[1]})

// Check if reloaded is set
if (queryDict['reloaded'] == 'undefined') {
    // Check if url contains any get-variables
    if (Object.keys(queryDict).length == 0) {
        var redirect = document.URL + '?reloaded';
    }
    else {
        var redirect = document.URL + '&reloaded';
    }

    // The timeout
    setTimeout(function () {
        window.location = redirect;
    }, 5000);
}

回答by Muhammad Kamran

You need to kept this script after doing something like insertions or updations or any other task then it will reload your page after 5 seconds for once. It works for me while uploading pictures i kept this scripts after uploading my file rather than kept it in window.load or documents.ready function its work for like a charm. here is my script. May this helps you solve your problem

您需要在执行插入或更新或任何其他任务后保留此脚本,然后它会在 5 秒后重新加载您的页面一次。它在上传图片时对我有用,我在上传我的文件后保留了这个脚本,而不是将它保存在 window.load 或 documents.ready 函数中,它的工作就像一个魅力。这是我的脚本。可以帮助你解决你的问题

<script type="text/javascript">
$(window).load(function () {
         $('#<%=FileUpload1.ClientID%>').uploadify({
        'swf': 'fy/uploadify.swf',
        'cancelImg': 'fy/uploadify-cancel.png',
        'buttonText': 'Browse Files',
        'uploader': 'Gallery.ashx',
        'folder': 'uploads',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',

        'multi':true,
        'auto': false,

        'formData':{'paid':<%= Request.QueryString["PhotoAlbumID"]  %>},
        'onUploadStart': function (file) {
            $("#<%=FileUpload1.ClientID%>").uploadify("settings", 'Txt', $('#password').val());

            $( document ).ready(function() {


            });  setTimeout(function() { window.location=window.location;},3000);

        }  
         });
});



</script>