V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
guyeuro
V2EX  ›  问与答

ajax 轮询如何设置次数和超时?

  •  
  •   guyeuro · 2017-08-15 11:56:16 +08:00 · 3639 次点击
    这是一个创建于 2459 天前的主题,其中的信息可能已经有所发展或是发生改变。

    譬如轮询 5 次后就不轮询了

    或者第一次轮询开始,

    超过 1 分钟后,就终止轮询

    9 条回复    2018-12-14 16:59:43 +08:00
    a570295535
        1
    a570295535  
       2017-08-15 12:14:03 +08:00
    function f(){
    //这里放上你的代码
    };
    for(var i = 1; i < 5; i++){//只能刷新 5 次
    setTimeout(f, 2000);//2 秒刷新一次
    }
    我也不知道行不行,你试试
    a570295535
        2
    a570295535  
       2017-08-15 12:16:44 +08:00
    var timesRun = 0;
    var interval = setInterval(function(){
    timesRun += 1;//每刷新一次 timesRun 就+1
    if(timesRun === 60){//如果它等于 60 次了,就删除这个 setInterval 循环
    clearInterval(interval);
    }
    //这里写你的代码
    }, 2000);
    a570295535
        3
    a570295535  
       2017-08-15 12:20:00 +08:00
    上面是根据次数,这次试试时间的:
    var startTime = new Date().getTime();//现在的时间
    var interval = setInterval(function(){
    if(new Date().getTime() - startTime > 60000){//现在的时间减去刷新开始的时间大于 60 秒
    clearInterval(interval);//就删除这个 setInterval 轮询
    return;
    }
    //这里写你的代码
    }, 2000);
    a570295535
        4
    a570295535  
       2017-08-15 12:38:13 +08:00
    测试成功,例子 1,限制次数:
    <script type="text/javascript" src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
    var timesRun = 0;//默认 timesRun 等于 0 次
    var interval = setInterval(function(){
    timesRun += 1;//每轮询一次 timesRun 就+1
    if(timesRun === 5){//轮询 5 次
    clearInterval(interval);//清除轮询
    }
    //我的代码
    function genHitokoto(){
    $.ajax({
    url: "https://sslapi.hitokoto.cn/?c=f&encode=text",//轮询地址
    type: 'GET',timeout: '3000',//3 秒超时
    success: function(data) {
    document.getElementById("hitokoto").innerHTML = data;//把一句话输出到 div
    },
    async: true,//异步请求
    });};genHitokoto();
    }//我的代码结束
    , 2000);//2 秒轮询一次
    </script>
    <div id="hitokoto"></div>
    a570295535
        5
    a570295535  
       2017-08-15 12:43:52 +08:00
    上面的 [}//我的代码结束] 写错行了,应该写到这个 [}] 的上面,
    测试成功,下面的代码是运行 60 秒的例子,全都很好用:
    <script type="text/javascript" src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
    var startTime = new Date().getTime();//现在的时间
    var interval = setInterval(function(){
    if(new Date().getTime() - startTime > 60000){//现在的时间减去刷新开始的时间大于 60 秒
    clearInterval(interval);//就删除这个 setInterval 轮询
    return;
    }
    //我的代码
    function genHitokoto(){
    $.ajax({
    url: "https://sslapi.hitokoto.cn/?c=f&encode=text",//轮询地址
    type: 'GET',timeout: '3000',//3 秒超时
    success: function(data) {
    document.getElementById("hitokoto").innerHTML = data;//把一句话输出到 div
    },
    async: true,//异步请求
    });};genHitokoto();
    //我的代码结束
    }, 2000);
    </script>
    <div id="hitokoto"></div>
    guyeuro
        6
    guyeuro  
    OP
       2017-08-16 16:13:55 +08:00
    guyeuro
        7
    guyeuro  
    OP
       2017-08-16 16:14:58 +08:00
    @a570295535

    var timesRun = 0;
    var interval = setInterval(function(){
    timesRun += 1;//每刷新一次 timesRun 就+1
    if(timesRun === 60){//如果它等于 60 次了,就删除这个 setInterval 循环
    clearInterval(interval);
    }
    //这里写你的代码
    }, 2000);
    ---------------------------------------------

    你这里这个 interval 变量能被函数里面的 clearInterval(interval) 调用??
    a570295535
        8
    a570295535  
       2017-08-16 16:16:27 +08:00
    @guyeuro 可以啊,你没看下面我的例子吗,都成功执行了啊
    yangtze
        9
    yangtze  
       2018-12-14 16:59:43 +08:00
    结合 Promise:

    ```
    let count = 5;
    const f = () => {
    return new Promise((resolve, reject) => {
    count--;
    resolve({ success: count > 0 ? false : true });
    });
    };

    const poll = fn => {
    const startTime = new Date().getTime() + 5000;
    let count = 100;
    let interval = setInterval(() => {
    if (new Date().getTime() > startTime || count === 0) {
    clearInterval(interval);
    return;
    }
    fn()
    .then(data => {
    if (data.success === true) {
    console.log("处理完成");
    clearInterval(interval);
    } else if (data.success === false) {
    console.log("还在处理中");
    }
    })
    .catch(err => {
    console.log(err);
    })
    .finally(() => {
    count--;
    });
    }, 300);
    };

    poll(f);
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1539 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:56 · PVG 07:56 · LAX 16:56 · JFK 19:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.