Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much of an effect can CPU have on JavaScript setInterval

I wrote a small jquery plugin that basically converts all words in an html element to spans, makes them invisible and then animates them into view. I made it so that you can define the time that it's supposed to take to load the whole element, and based on tests the math seems to be right, but in practice it takes quite a bit longer.

See jsfiddle: http://jsfiddle.net/A2DNN/

Note the variables "per" and "ms", this basically tells it to process "per" number of words every "ms" milliseconds.

In the log you'll see it'll process 1 word ever 1 ms, which should result in a MUCH faster loading time.

So I'm just wondering, is it possible that the CPU is forming a bottleneck here? In that JS is fading items into view, which is handled by the CPU, which isn't very fast at graphical processing.

It sounds almost silly to ask, I'd expect these days a CPU would laugh at a small bit of work load like this.

like image 378
Naatan Avatar asked Oct 24 '22 22:10

Naatan


1 Answers

It is due to a minimum timeout forced by the browser's JavaScript implementation. You can't have a 1ms timeout, it is slightly more than that. There has already been a discussion about that here.

like image 115
Igor Zinov'yev Avatar answered Oct 27 '22 10:10

Igor Zinov'yev