简体   繁体   English

jQuery轮询请求仅在变量!='something'时

[英]Jquery poll request only if variable != 'something'

I have this code: 我有以下代码:

<script>
    $(function() {
        (function poll(){
            setTimeout(function(){
                var process_id = $("#form3_progress_process_id").val();
                if(process_id != 'undefined') {
                    $.ajax({
                        type: "POST",
                        url: "grab.php",
                        data: "action=step3_progress&process_id=" + process_id,
                        success: function(data){
                            console.log("successful");
                        }, dataType: "json", complete: poll, timeout: 30000 });
                }
            }, 2000);
        })()
    });
</script>
<input type="hidden" id="form3_progress_process_id" value="undefined">

What i want is that it only sends the query if the value from the input form3_progress_process_id is not ' undefined '. 我想要的是,如果来自输入form3_progress_process_id的值不是' undefined ',则它仅发送查询。

At the start it is " undefined " but after some time it get's changed with jquery. 一开始它是“ undefined ”,但一段时间后它被jquery更改了。

However, the above code does not work for any reason. 但是,以上代码由于任何原因都不起作用。 It does not start sending the request after the value has been changed. 更改值后,它不会开始发送请求。

What did i wrong? 我怎么了

Your code will fire only if the input changes in the first 2 seconds from the begining. 仅在input从头开始的2秒内发生更改时,您的代码才会触发。 You should add a call to the poll method in your else , so the cycle doesn't break: 您应该在else添加对poll方法的调用,这样循环不会中断:

$(function() {
    (function poll(){
        setTimeout(function(){
            var process_id = $("#form3_progress_process_id").val();
            if(process_id != 'undefined') {
                $.ajax({
                    type: "POST",
                    url: "grab.php",
                    data: "action=step3_progress&process_id=" + process_id,
                    success: function(data){
                        console.log("successful");
                    }, dataType: "json", complete: poll, timeout: 30000 });
            }else{
                poll();  ////Add this!!
            }
        }, 2000);
    })()
});

Cheers, from La Paz, Bolivia 来自玻利维亚拉巴斯的欢呼声

Unless your hidden input changes within the 2 seconds, the timer will not fire again. 除非您的隐藏输入在2秒钟内发生变化,否则计时器将不会再次触发。 change to setInterval 更改为setInterval

 setInterval(function(){
            var process_id = $("#form3_progress_process_id").val();
            if(process_id != 'undefined') {
                $.ajax({
                    type: "POST",
                    url: "grab.php",
                    data: "action=step3_progress&process_id=" + process_id,
                    success: function(data){
                        console.log("successful");
                    }, dataType: "json", complete: poll, timeout: 30000 });
            }
        }, 2000);
    })()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM