延迟关闭窗口javascript
我正在编写一个 Google Chrome 扩展。感谢这里的每个人忍受我的愚蠢问题。该例程很原始,但运行良好。唯一的问题是它运行得太快,导致服务器超载并且我的 IP 地址被阻止。所以需要节流阀。
问题是用计时器构造东西好还是用 setInterval 构造东西更好。检查特定页面后,内容脚本使用 self.close() 关闭其窗口。如果我将其放入 setInterval 中,我可以延迟页面的关闭,这会减慢整个过程的速度,其程度与间隔的长度一样。看起来油门不错。
现在内容脚本的最后一行很简单:
self.close();
我认为如果我按如下方式修改代码,我会得到延迟:
var t=setTimeout("self.close()",2000);
这会起作用吗?有更好的方法吗?
I am writing a Google Chrome extension. Thanks to everyone here for putting up with my silly-assed questions. The routine is primitive but runs fine. The only problem is that it runs so fast that it overloads the server and my ip address gets blocked. So it needs a throttle.
The question is whether it is better to construct something with a timer or with setInterval. After examining a particular page, the content script closes its window with self.close(). If I put this into a setInterval, I could delay the closing of the page and this would slow the whole process as much as the length of the interval. Seems like a good throttle.
Now the last line of the content script is simply:
self.close();
I presume that if I modify the code as follows I would get my delay:
var t=setTimeout("self.close()",2000);
Will this work? Is there a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我宁愿使用:
但你的方式也有效......
I'd rather use :
But your way is valid too...
如果页面关闭是等待的适当时间,那么这是完全可以的。在这种情况下,因为它看起来是一个相关的地方,那么我认为你没问题。尽管我会使用克里斯托弗的建议。
如果您的处理时间比间隔长,使用 setinterval 定期运行它们会遇到问题 - 因为这似乎涉及打开和关闭页面,它可能。
通常,setInterval 适合定期执行小流程。在这种情况下,您只想在处理中等待,这对我来说 setTimeout 更好。
If the closing of the page is an appropriate point to wait, then this is perfectly fine. In this case, because it would appear to be a relevant place, then I think you are fine. Although I would use Christophes suggestion.
Using a setinterval to run them periodically will run into problems if your processing takes longer than the interval - as this seems to involve opening and closing pages, it could.
As a rule, setInterval is good for doing small processes regularly. In this case, you just want to put a wait into the processing, which suggests to me that setTimeout is better.