Javascript 在加载时运行一个函数,然后每 10 分钟运行一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6513078/
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
Run a function on load and then every 10 minutes
提问by Nikon0266
Is there a way to execute a javascript function when the page loads and then not again for another 10 minutes. Even if user browses to another page? If they dont want the function to run again until the 10 minutes is up?
有没有办法在页面加载时执行 javascript 函数,然后再过 10 分钟不再执行。即使用户浏览到另一个页面?如果他们不希望该功能再次运行,直到 10 分钟结束?
回答by Jedediah
Simply create the function you want to execute onload, call it once on DOM ready, and then use setTimeout()
只需创建要在 onload 上执行的函数,在 DOM 就绪时调用一次,然后使用 setTimeout()
<script type="text/javascript">
function doSomething() {
alert("Doing something here");
}
window.onload = function () {
doSomething(); //Make sure the function fires as soon as the page is loaded
setTimeout(doSomething, 600000); //Then set it to run again after ten minutes
}
</script>
Edit: Didn't notice the part about even if they're on another page. If it's another page on your website, you could set a cookie once the page loads, and include a timestamp for when the cookie was set. In the window.onload of the rest of your pages, you can then read that timestamp, and calculate whether 10 minutes have passed or not. If it has, call the function again.
编辑:即使它们在另一页上,也没有注意到有关的部分。如果它是您网站上的另一个页面,您可以在页面加载后设置 cookie,并包含设置 cookie 的时间戳。在其余页面的 window.onload 中,您可以读取该时间戳,并计算是否已过去 10 分钟。如果有,请再次调用该函数。
Edit: 10 minutes is 600000 milliseconds
编辑:10 分钟是 600000 毫秒
回答by Pekka
Even if user browses to another page?
即使用户浏览到另一个页面?
Nope. Once your page closes, you no longer have control over what happens.
不。一旦您的页面关闭,您就无法再控制发生的事情。
回答by Alnitak
Use setInterval
or setTimeout
to make something happen in the future.
使用setInterval
或setTimeout
使某事在未来发生。
But as soon as the user leaves your page those timers will be cancelled - the entire content will be unloaded.
但是一旦用户离开您的页面,这些计时器将被取消 - 整个内容将被卸载。
回答by Kalaivani M
function callfunction () {
// do something
}
window.onload = function () {
// Initial function call
callfunction();
setInterval(function () {
// Invoke function every 10 minutes
callfunction();
}, 600000);
}
回答by christian
you're required to save the state in a remote server.
您需要将状态保存在远程服务器中。
this is your steps:
这是你的步骤:
the javascript function loads, no matter where or when, it loads.
when loaded (the javascript fn) calls a remote server using a UNIQUEID, that can be obtained via cookies, or session variable, from the loggedin user id...whatever.
you send a request to a server using such UNIQUEID (via ajax, normally), your server will tell you if such process must start again or you should avoid it (a page jump as an example or a page refresh in a different browser using the same login, it depends on your business logic).
the response from step3 is used to execute/initialize the javascript loader or not.
javascript 函数加载,无论何时何地,它都会加载。
加载时(javascript fn)使用 UNIQUEID 调用远程服务器,该 ID 可以通过 cookie 或会话变量从登录的用户 ID 中获取......无论如何。
您使用此类 UNIQUEID(通常通过 ajax)向服务器发送请求,您的服务器会告诉您此类进程是否必须再次启动或您应该避免它(例如页面跳转或使用不同浏览器中的页面刷新)相同的登录名,这取决于您的业务逻辑)。
来自 step3 的响应用于执行/初始化 javascript 加载器与否。
回答by ninjagecko
If the user visits another website in the same tab, you're out of luck; once your webpage is closed the javascript dies.
如果用户访问同一选项卡中的另一个网站,那么您就不走运了;一旦您的网页关闭,javascript 就会消失。
However you're in luck if the user is just visiting other pages on yourwebsite. You can do this as follows... but it requires a fundamental redesign of your website. You'd have to use a framework which wraps all link-clicks in XHRs (XML HTTP Requests) and replaces the page with the new data, giving you the illusion that you are clicking links (which you are, but the browser never refreshes the page). This can be done with an iframe or not. This is how GMail works.
但是,如果用户只是访问您网站上的其他页面,那么您很幸运。您可以按如下方式执行此操作...但它需要对您的网站进行根本性的重新设计。您必须使用一个框架,该框架将所有链接点击都封装在 XHR(XML HTTP 请求)中,并用新数据替换页面,让您产生点击链接的错觉(确实如此,但浏览器永远不会刷新页)。这可以使用 iframe 或不使用 iframe 来完成。这就是 GMail 的工作原理。
You could code such a framework yourself, but you're better off using a framework which has url-rewriting support for faking a history (or else your website won't work well with user histories).
您可以自己编写这样的框架,但最好使用具有 url 重写支持伪造历史记录的框架(否则您的网站将无法很好地处理用户历史记录)。
To answer your question about the "every 10 minutes", use setInterval(myFunction, 10*60*1000)
(which may have issues in some browsers due to optimizations).
要回答有关“每 10 分钟”的问题,请使用setInterval(myFunction, 10*60*1000)
(由于优化,某些浏览器可能会出现问题)。
回答by paezinc
Assuming that you use a server language, you can set a session variable at startup (startup date/time) and then, using setInterval, compare current time with startup date/time.
假设您使用服务器语言,您可以在启动时设置会话变量(启动日期/时间),然后使用 setInterval 将当前时间与启动日期/时间进行比较。
回答by Wicked Coder
You can use:
您可以使用:
setTimeout(alert('After 10 secs'),10000);