javascript Greasemonkey 脚本每分钟重新加载页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25484978/
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-28 04:34:14  来源:igfitidea点击:

Greasemonkey script to reload the page every minute

javascriptgreasemonkeyreload

提问by John Smith

How to reload a page every 60 seconds?

如何每 60 秒重新加载一个页面?

My attempt:

我的尝试:

setTimeout (location.reload, 1 * 60 * 60);

I'm not sure what those numbers mean, or how to adapt them to reload after 60 seconds only.

我不确定这些数字是什么意思,或者如何使它们仅在 60 秒后重新加载。

回答by Aryess

setTimeout(function(){ location.reload(); }, 60*1000);

You have to pass a full function as first argument, and second is the time in millisecond. So in your case, 60 * 1000

你必须传递一个完整的函数作为第一个参数,第二个是以毫秒为单位的时间。所以在你的情况下,60 * 1000

回答by Quasimodo's clone

You may give a function name, since it is a callable, however, location.reloadis a method of the locationobject. It is callable, but when it is executed by the timer, the thiscontext will not be the location object. This fact leads to an error.

你可以给一个函数名,因为它是一个可调用的,但是,它是对象的location.reload一个方法location。它是可调用的,但是当它被定时器执行时,this上下文将不是位置对象。这个事实会导致错误。

The solutions are:
Write a simple anonymous function as a wrapper as already described in the accepted answer or
create a bound function of the reload method with locationas its thiscontext:

解决方案是:
编写一个简单的匿名函数作为包装器,如已接受的答案中所述,或者
创建一个 reload 方法的绑定函数location作为其this上下文:

setTimeout(location.reload.bind(location), 60000);

回答by Philipp

Looking at a documentationmight help you. The parameters to the setTimeout function are the action which is performed and the number of milliseconds until this happens. 1 * 60 * 60is 3600ms or 3.6 seconds. A timespan of 60 seconds would be 60000.

查看文档可能对您有所帮助。setTimeout 函数的参数是执行的操作以及执行此操作之前的毫秒数。1 * 60 * 60是 3600 毫秒或 3.6 秒。60 秒的时间跨度将是 60000。