当前位置:网站首页>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 .

边栏推荐
- (problem solving) missing gcr io/kubebuilder/kube-rbac-proxy:v0.8.0
- Figure database ongdb release v-1.0.2
- Classification of traffic signs
- sequelize 常用命令使用
- [nvme2.0b 5] sous - système nvm
- 为什么程序员写的第一个程序是“Hello World!”
- fastdfs-5.0.5安装
- 【NVMe2.0b 11】NVMe Reset
- Harmonyos Hongmeng uses ORM bee to access database instances
- Image metadata acquisition and modification
猜你喜欢

Scheduling function: Splunk operator Controller Manager

TX2挂载SD卡,jtop

不规范的命名

Typora + picgo configure the drawing bed to realize automatic uploading of pictures

Check information on the Internet after the college entrance examination, and pay attention to prevent websites without SSL certificates

深度学习期末复习

【NVMe2.0b 5】NVM Subsystem

Vs loading symbols causes program to start slowly

六、MySQL之数据定义语言(一)

uv_ loop_ Init() process
随机推荐
Redis configuration and optimization
克隆的vmware虚拟主机网卡无法启动解决办法
Typora + picgo configure the drawing bed to realize automatic uploading of pictures
为什么在高并发下很容易就被setInterval给坑了
uv_run流程
2022 low voltage electrician test question simulation test question bank and answers
Splunk: Auto load Balanced TCP Output issue
EU5, eu7, EX3, Ex5 install third-party apps
mysql-索引创建、优化分析、索引优化
Harmonyos Hongmeng uses ORM bee to access database instances
Redis6.0 new features (Part 2)
图书馆管理系统(PHP期末报告)
[QNX Hypervisor 2.2用户手册]5.5 启动和使用Guest
【leetcode周赛总结】LeetCode第298场周赛总结(6.19)
【NVMe2.0b 9】控制器初始化过程
R data analysis: significance and practice of calibration curve and DCA curve in clinical prediction model
你是一名技术管理者还是项目管理者?
php使用composer
golang标准库time
【NVMe2.0b 8】NVMe 队列仲裁机制