定时器轮询

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

你可能感兴趣的:(定时器轮询)