Skip some code if the computer is slow

后端 未结 6 926
旧时难觅i
旧时难觅i 2021-02-04 09:27

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 kno

相关标签:
6条回答
  • 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.

    0 讨论(0)
  • 2021-02-04 09:30

    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.

    0 讨论(0)
  • 2021-02-04 09:31

    i think there are two options:

    1. Let the user decide - give a 'low bandwidth/low spec' option to the user which when clicked will display the simpler version of the site.

    2. Try to detect slow machines - you could try to detect a slow machine by using a timeout script - if the animation/loading doesn't complete within a certain time switch to the simpler version of the site. The downside to this method is that you have no idea why the script timed out - perhaps it was a bad connection or the user was loading something else at the same time temporarily slowing their machine.

    Hope this gives you some ideas.

    Josh

    0 讨论(0)
  • 2021-02-04 09:31

    Given that JavaScript runs in a partial trust environment, you're not going to be able to get any OS-level info, I would think.

    The best you can do really is test "responsiveness". A possible approach would be just to start playing the full animation and detect lag by comparing the target frame rate with "real" frame rate. You could then simply skip bits of the animation (code) to reduce the computation time and thus improve the responsiveness/frame-rate accordingly. This is essentially how many desktop games detect lag and thus reduce the complexity of animations. I can't guarantee it will work equally well in the browser with JavaScript, but it's worth a try certainly.

    0 讨论(0)
  • 2021-02-04 09:38

    I'm guessing that the code which you want to switch off takes a significant amount of time to run.

    I suggest that you:

    • Begin to execute this code
    • After it's partially executed, see how much real-time has ellapsed
    • If too much real-time has ellapsed (i.e. if the code is executing too slowly) then cancel execution instead of finishing it.
    0 讨论(0)
  • 2021-02-04 09:42

    I would like to point out that if you're going to leave it up to the user (which I think is a good idea) you should make it clear that you're asking about computer speed, not connection speed or bandwidth. A user may have a very fast laptop, but be working on a slow public wi-fi connection, or vice versa. Since Javascript is client-side, it's only the computer speed that matters.

    0 讨论(0)
提交回复
热议问题