如果计算机速度慢,请跳过一些代码 [英] Skip some code if the computer is slow

查看:111
本文介绍了如果计算机速度慢,请跳过一些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检测计算机是否运行缓慢并且没有运行某些代码(通过关闭jQuery动画或仅运行 快速运行函数)?

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.

推荐答案

运行javascript时,您无需事先了解目标计算机的性能。我唯一能想到的就是运行一个函数进行一些计算并测量所花费的时间。该功能必须进行足够数量的计算,以确保运行所花费的时间代表机器的性能。

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.

这篇关于如果计算机速度慢,请跳过一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆