当前位置:网站首页>Why is setinterval so easy to get stuck in the high and low level

Why is setinterval so easy to get stuck in the high and low level

2022-06-22 03:26:00 Lao Zhu Yubing

  Most programmers are setInterval Let's go , If not high concurrency , Program execution is fast , Maybe I don't know what's going on . 

  Simulate a scene ,  If we need one client.html , Every second , Back to the server sleep.php Time after processing .

This demand is very simple , Go straight to the code . client.html The code is as follows

<html>
<head>
<meta charset="utf-8">
</head>
<script src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<body>

<script type="text/javascript">
function test(){
	$.get('sleep.php',function(data){console.log(data);});
}
setInterval(test,1000);
</script>
</body>
</html>

Server program sleep.php as follows

<?php
echo date('H:i:s');
?>

Open with a browser htpt://localhost/client.html , And open the console , The program works well , Perfect .

One day the program was especially popular , The number of users has soared to hundreds of thousands , sleep The program has also become more complex , Add a complex business such as billing system , Lead to sleep.php The execution time is very long , Simulate sleep.php need 3 second . in order to sleep.php good-looking , The old Zhu code is changed to the following .

<?php
while(true){
   if(time()%3==0){
		echo date('H:i:s');
		exit;
   }
   sleep(1);
}

?>

This time refresh http://localhost/client.html , Look at the console , Something amazing happened . Output every three seconds , But the output time becomes 3 individual , Look at the part marked in red . That is to say, the client initiates 3 Two concurrent connections , Reality is sleep.php The longer it takes to execute ,client.html The shorter the interval between requests , Resulting in more concurrency ( Unless the number of browser connections is limited ) . For high concurrency , It's likely to avalanche .

So how to avoid this situation ? How can I really get data from the server every second ? Have a look first interval and setTimeout The difference between! .

setInterval

setInterval() Method can call a function or evaluate an expression according to a specified cycle ( In Milliseconds )

grammar :

setInterval( Function expression , Number of milliseconds );

setInterval() Will keep calling functions , until clearInterval() Called or window closed , from setInterval() Back to ID Value can be used as clearInterval() Method parameters .

setTimeout

setTimeout() Method is used to call a function or evaluate an expression after a specified number of milliseconds ( In Milliseconds )

grammar :

setTimeout( Function expression , Number of milliseconds );

setTimeout() Execute the function only once , If you need multiple calls, you can use setInterval(), Or call again in the function body setTimeout()

difference

   From the above analysis, we can see ,setTimeout And setInterval The main difference is :

  setTimeout() Method only runs once , In other words, when the set time is reached, start to run the specified code , After running, it's over , If you want to execute the same function again , You can call... Again in the function body setTimeout(), It can achieve the effect of circular call .

  setInterval() It is executed in a loop , That is, the corresponding function or expression is executed every time the specified time interval is reached , It's a real timer .

 

Understand the difference between the two functions ,client.html This program can be changed a little .

<html>
<head>
<meta charset="utf-8">
</head>
<script src="//libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
<body>

<script type="text/javascript">
setTimeout(test,1000);
function test(){
	$.get('sleep.php',function(data){
		console.log(data);
        setTimeout(test,1000);
	});
}
</script>
</body>
</html>

Refresh the page again , Concurrent disappearance , And on the server sleep.php After execution , Then request the contents of the server .

 

原网站

版权声明
本文为[Lao Zhu Yubing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220307007009.html