简体   繁体   English

Javascript SetTimeOut会影响页面性能吗?

[英]Does Javascript SetTimeOut Affect Page Performance?

I am writing an AJAX script that basically calls a PHP page and requests some information. 我正在编写一个AJAX脚本,它基本上调用PHP页面并请求一些信息。 The PHP page queries a database. PHP页面查询数据库。 I want to set the page to make the call every 5 minutes, but the only way I know how to do so is by using the settimeout function. 我想将页面设置为每5分钟拨打一次电话,但我知道如何操作的唯一方法是使用settimeout函数。 I am wondering if this settimeout function is constantly running is this going to be harsh on the page's performance? 我想知道这个settimeout功能是否一直在运行,这会对页面的性能造成严重影响吗? is there another way to go about this? 还有另一种方法可以解决这个问题吗?

use setInterval . 使用setInterval They should render the same results (~0 performance loss), but setInterval is the obvious way to go. 它们应该呈现相同的结果(~0性能损失),但setInterval是显而易见的方法。

Think about Google Wave: they send an AJAX request for every letter you type, yet it doesn't feel slow at all, so one request every 5 minutes is nothing. 想想Google Wave:他们会为您输入的每个字母发送一个AJAX请求,但一点也不觉得很慢,因此每5分钟发送一个请求就没什么了。

I don't really thing the problem is setTimeout, perhaps it is the way you have built the script. 我真的不是问题是setTimeout,也许是你构建脚本的方式。

In fact, most of the time setTimeout it's preferred, for a reason, setTimeout will wait the execution of your script, setInterval will execute in every interval without waiting. 实际上,大部分时间setTimeout都是首选,出于某种原因,setTimeout会等待你的脚本执行,setInterval会在每个时间间隔内执行而无需等待。

Pls look here at the bottom: 请看这里的底部:

https://developer.mozilla.org/En/Window.setInterval https://developer.mozilla.org/En/Window.setInterval

If there is a possibility that your logic could take longer to execute than the interval time, it is recommended that you recursively call a named function using window.setTimeout.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM