梅川酷芷 2018-06-03 18:11 采纳率: 50%
浏览 1499
已采纳

js 轮询API取返回值判断

哪位大哥可以在我下面的基础上给我修改一下呀。
就是需要JS 1秒一次轮询API,取返回json的值,判断JSON里qr_code不为null,执行qr_code的值。继续轮询直至一分钟后无结果停止轮询。提示刷新。

这是我的JS代码

                  function agreeGotoTb(btn) {
            $(btn).attr("disabled", "disabled").val("请等待...");

        var app = new Vue({
            el: '#app',
            data: {
                json_file: '',
                uuid:'<?php echo $uuid;?>'
            },
            methods: {
                getData: function () {
                    var self = this;
                    reqwest({
                        url: 'http://111.21.189.33:8000/redirect',
                        type: 'json',
                        method: 'get',
                        crossOrigin: true,
                        data: {uuid:this.uuid},
                        success: function (resp) {
                            self.json_file = resp;
                             window.location.href= self.json_file['qr_code'];
                        }
                    })
                }
            },
          created: function () {
            this.getData();
          }
        })
        }
  • 写回答

6条回答 默认 最新

  • Go 旅城通票 2018-06-04 00:22
    关注
    
        function agreeGotoTb(btn) {
            $(btn).attr("disabled", "disabled").val("请等待...");
    
            var app = new Vue({
                el: '#app',
                data: {
                    json_file: '',
                    uuid: '<?php echo $uuid;?>',
                    timer:null,
                    count:0///////////计数
                },
                methods: {
                    getData: function () {
                        var self = this;
                        reqwest({
                            url: 'http://111.21.189.33:8000/redirect',
                            type: 'json',
                            method: 'get',
                            crossOrigin: true,
                            data: { uuid: this.uuid },
                            success: function (resp) {
                                self.json_file = resp;
                                clearInterval(self.timer)
                                window.location.href = self.json_file['qr_code'];
                            }
                        })
                    }
                },
                created: function () {
                    this.getData();
                    //计时器轮训
                    var me = this;
                    this.timer = setInterval(function () {
                        me.getDate();
                        me.count++;
                        //超过60次停止计时器
                        if (self.count >= 60) {
                            clearInterval(self.timer);
                            alert('无结果,请刷新页面')
                        }
                    }, 1000);
                }
            })
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料