A bug I ran into last night.

Source: Internet
Author: User

Tonight I ran into a bug, in the Refresh page when the browser occupied by the memory is constantly rising, chrome process CPU occupancy rate is also maintained around 50, do not know what is the reason
Looking for a long time, commented that most of the code part of the slowly ruled out, finally found that the error is JavaScript, it is not clear to look at what is the reason, wrote a basic JS code to continue to loop, finally found that the call to use the function of the problem
SetTimeout ("ShowTime ()", 1000);
SetInterval ("ShowTime ()", 1000);
These two functions are called every 1 seconds, but the following one will accumulate memory consumption, and the above is the memory resource is unchanged.
SetTimeout () This function only executes one code at a time, and setinterval () does not stop until the clearinterval () or window is closed until the function is called. This results in a large amount of memory resources being consumed.
Because I do not understand JS, so the understanding is not very deep
<script language= "JavaScript" >function ShowTime () {var date=new date (); var hours=date.gethours (); var minutes= Date.getminutes (); var seconds=date.getseconds (); if (hours.tostring (). length = = 1) {   hours= ' 0 ' +hours;} if (minutes.tostring (). length = = 1) {   minutes= ' 0 ' +minutes;} if (seconds.tostring (). length = = 1) {   seconds= ' 0 ' +seconds;} var formattime=hours+ ":" +minutes+ ":" +seconds;settimeout ("ShowTime ()", "N"),/*setinterval ("ShowTime ()", 1000); */ document.getElementById ("Time"). Innerhtml=formattime;} </script>

A bug I ran into last night.

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.