定时器轮询

1.js  定时器简单的轮询方式

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6     </head>
 7     <body>
 8         <script>
 9 
10           // 轮询方法
11                     function polling(timestamp,callback){
12                         var flag = 0,timestamp = timestamp || 5000,times=0,s,
13                         func=function(timestamp,flag){
14                             if(timestamp == 1000){
15                                 clearTimeout(s);
16                                 if(times == 10){
17                                      clearInterval(s);
18                                     return;
19                                 }else{
20                                     times++;
21                                     s = setInterval(function(){
22                                         callback();
23                                         func(timestamp,flag);
24                                     },1000)
25                                 }
26                             }else{
27                                 clearInterval(s);
28                                 callback();
29                                 flag++;
30                                 if(flag % 2 !== 0){
31                                     timestamp-=1000;
32                                 }
33                                 console.log(timestamp,flag,123)
34                                 s=setTimeout(function(){
35                                     func(timestamp,flag)
36                                 },timestamp);
37                             }
38                         }
39                         func(timestamp,flag);
40                     }
41           // 这里定义轮询请求方法
42                     function callback(){
43                         console.log(1111);
44                     }
45           // 传入倒计时,每请求两次  间隔减少1s
46                     polling(5000,callback);
47     </script>
48     </body>
49 </html>

 

转载于:https://www.cnblogs.com/chengyunshen/p/7852723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值