Javascript clock interval problem ~

Source: Internet
Author: User

In JavaScript, setTimeout () and setinterval () are used to create and manage the clock. however, there is no reference to the accuracy of the clock. before discussing specific issues, we should first describe the response of setTimeout and setinterval when the clock interval is 0.

When the user sets setTimeout (Proc, 0), the CPU will be transferred out until the next clock interval starts to execute proc, rather than "execute proc after 0 ms ";

When you set setinterval (Proc, 0), the clock will not be initialized. Javascript requires at least 1 ms to create the clock.

Next, when the interval value is 1 (or another small value), the clock is triggered at a "minimum" interval, which is the same as setTimeout (Proc, 0) the time interval is the same value.

The following code is used to check the value:

<SCRIPT>
Dates = new array (100000 );
Count = 0;
Tid = setinterval (function () {dates [count ++] = new date ()}, 1 );

// Click the IE document area to display the result.
Document. onclick = function (){
Clearinterval (TID );

VaR STR = '';
For (VAR I = 1; I <count; I ++ ){
STR + = 'sleeped: '+ (dates [I]. valueof ()-dates [I-1]. valueof () + 'Ms <br> ';
}
Document. writeln (STR );
}
</SCRIPT>

The output value of this Code in IE is 15 ~ 16 Ms. considering the consumption of script code execution, I think the interval is 16 Ms. however, in Firefox, the displayed value may also include 0 ms. this should be because in Firefox, the internal clock of the Java engine is used, rather than the operating system clock. therefore, it should be related to the Java clock processing mechanism (or even version problems ). but even in Firefox, there will be no 2 ~ These values in 14 indicate that the two engines are algorithm-consistent: 16 Ms as the interval.

This value is mentioned in other documents of some countries. For example:

Http://www.codingforums.com/archive/index.php? T-46607.html

Here we mention "Win9x and winme have 55 MS as the smallest frequency that will not cause a lag. it's 10 MS In the WINNT/2 k/XP/2K3 versions. I can't answer for what limits are in place in Mac OS 9, Mac OS X or Linux systems. "10 ms is clearly inaccurate. I think it will be a false pen of 0x10 ms.

The minimum value of 10 ms is also mentioned in the official WebKit internal forum. Since I do not use the WebKit engine (Safari for Mac), I cannot perform more tests.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.