Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip some code if the computer is slow

Is there any way to detect if a computer is slow and not run some code (by either turning jQuery animations off or just running a function if it is fast)?

I know this question is probably really trivial, but I noticed that on some slower computers even the simplest margin animation to move something is done in flashes that doesn't look very nice.

Update:
The code I'm trying to run is simply a bunch of animations; they all take the same amount of time but on slower browsers the animation is segmented like what you see when you watch a video that is buffering.

like image 589
Brandon Wang Avatar asked Jun 28 '09 22:06

Brandon Wang


2 Answers

When running javascript, you don't have the luxury of knowing the target computer's performance beforehand. The only thing I can think of, would be to run a function doing some calculations and measuring the time taken. The function must do a sufficient number of calculations in order to make sure that the time taken to run, is representative of the performance of the machine.

In general, I would advise against doing such a performance test, because it takes resources on the target machine, something that users generally don't like. But perhaps you could measure the time taken to complete the first animation, and if it is too slow, disable subsequent ones.

like image 178
driis Avatar answered Oct 04 '22 03:10

driis


The most simple solution would be to leave it up to the user with a check box, but other wise you could try timing the first animation for a computer and then if it exceeds a certain time limit, the remaining animations are turned off... for example...

var start = new Date();
//... some jQuery animation
var end = new Date();
var diff = end - start;

Then, for example, if the animation should take 1.5 seconds, and the time difference is like 5 seconds, then turn off any remaining animations.

like image 31
hugoware Avatar answered Oct 04 '22 04:10

hugoware