开发者

Javascript setInverval seems slow?

So I am trying to do a setInterval that checks the placement of something on the page. Issue I am having is that if I set it for 10, 100, 250 it seems to be very slow and slows scrolling down.

setInterval('functionName()', 10);
setInterval('functionName()', 100);
setInterval('functionName()', 250);

UPDATE: The reason for the function is to move an element down the page when the user scrolls. The issue is that it is a Facebook app, in an iFrame, that is larger than the screen. I can't seem to find a way to attach the onScroll property outside of the iFrame, to Facebook (or maybe you can?!? I haven't had any luck yet) so I am being forced to use this method. Basically, it will grab the scroll height that Facebook passes in and move the element down the page. It works...but it seems to slow even scrolling do开发者_StackOverflow社区wn.

Is this something I just can't get around?


You're doing something every 10ms. That very well could intefere with the normal operation of the browser. The browser interface is blocked whenever javascript is running.

Perhaps if you describe what you're trying to do with the interval timers and post your code, we can suggest better performing ways to do that.


The minimum time resolution for setInterval() and setTimeout() varies by browser, and in some cases can be as much as 15 or 20 ms (some articles state up to 75ms). Your attempt to perform an action at higher resolutions (shorter intervals) is really more of a request to the browser -- there's no guarantee. Requests at resolutions higher than the browser's capability will be "clamped", and run no more frequently than that minimum. And of course, if the browser's workload is sufficiently high (as it is likely to be given the assurance of a continually called function), the interval may be significantly greater.

As a side note, it's best not to provide your function argument to setInterval() as a string. Doing so will cause that function be evaluated in the global scope. Instead of this:

setInterval('functionName()', 10);

you should use this:

setInterval(functionName, 10);

You should reconsider the need for running your function so frequently. Perhaps there's an event-driven alternative.

References: MDN on minimum delay


JavaScript is, by it's very nature, single threaded. That means that your JavaScript runs in the same process as your browser's UI.

So, whenever the browser is executing your JavaScript, it has to halt whatever it is that it's doing (which, quite often, might be drawing the page in the browser window), and run your code.

Since you are running a piece of code 100 times per second (1000 / 10), you are simply hindering your browsers normal form of operation!!


My guess is that functionName() is slow. 250 shouldn't be too bad for modern browsers. Post the function and we'll see if we can improve it.


Something that hasn't been mentioned: you're passing a string to setInterval when you really should be passing the function object itself:

setInterval(myFunction, 5000)

What happens when you give it a string like "functionName()" is that it's eval'ed, that might be the source of some of the slowness.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜