简体   繁体   English

如何在ajax请求完成后1秒递归调用函数?

[英]How to recursively call a function 1 second after an ajax request is done?

I have a case where I do not want to call a method every second. 我有一个案例,我不想每秒调用一个方法。 Rather, I want to call a function 1 second after the ajax request is completed regardless of the result. 相反,我想在完成ajax请求后1秒钟调用一个函数,而不管结果如何。

I tried to use the $.delay() function but it gives me an error 我试图使用$.delay()函数,但它给了我一个错误

TypeError: $.delay is not a function

Here is my code 这是我的代码

$(function(){
    function checkMessages(){

        $.ajax({
            type: 'GET',
            url: icwsHandlerUrl,        
            data: {method: 'getMessages', jSON: true},
            dataType: 'json',
            cache: false,
            timeout: 5000,
            success: function(data) {
                console.log(data); // for testing only but this should call a handler for the data  
            },
            complete: function(){
                $.delay(1000, function(){
                    checkMessages();
                });
            }
        });

    }

    $(window).load(function(){
        checkMessages();
    });
});

Note: I am using jQuery version 2.1.0 注意:我使用的是jQuery 2.1.0版

$.delay is used to delay events which are stored in jQuery's event queue. $.delay用于延迟存储在jQuery事件队列中的事件。 For this you should use JavaScript's own setTimeout method instead: 为此,您应该使用JavaScript自己的setTimeout方法:

setTimeout(checkMessages, 1000);

Try chaining all of this like this: 尝试将所有这些链接起来:

$(function(){
    function checkMessages(){

        $.ajax({
            type: 'GET',
            url: icwsHandlerUrl,        
            data: {method: 'getMessages', jSON: true},
            dataType: 'json',
            cache: false,
            timeout: 5000,
            success: function(data) {
                console.log(data); // for testing only but this should call a handler for the data  
            },
            complete: function(){
                $.delay(1000, function(){
                    checkMessages();
                });
            }
        });

    }

    $(window).load(function(){
        checkMessages();
    });
});

into this: 进入这个:

var req = $.ajax({
                type: 'GET',
                url: icwsHandlerUrl,        
                data: {method: 'getMessages', jSON: true},
                dataType: 'json',
                cache: false,
                timeout: 5000,
                success: function(data) {
                    console.log(data); // for testing only but this should call a handler for the data  
                },
                complete: function(){
                    $.delay(1000, function(){
                        checkMessages();
                    });
                }
            });

var nextTask = req.then(function(data){something});
nextTask.done(function(data){somethingelse});

the above code will wait everything to be done to do the next task. 上面的代码将等待完成下一个任务所需的一切。 Try it out. 试试看。

Make sure you remove the $.delay() and put it into the req.then() function. 确保删除$.delay()并将其放入req.then()函数中。

Here is the .then() explanation https://api.jquery.com/deferred.then/ 这是.then()说明https://api.jquery.com/deferred.then/

EDIT: 编辑:

$(window).load(function{
     $.delay(1000, function(){
         checkMessages();
     });
});

and place check messages outside of this piece of code. 并在这段代码之外放置检查消息。

The previous answers contain all the essential bits, but here's a complete solution: 之前的答案包含所有基本位,但这是一个完整的解决方案:

$(function(){
    var checkMessagesTimeout;

    function checkMessages(){

        $.ajax({
            type: 'GET',
            url: icwsHandlerUrl,        
            data: {method: 'getMessages', jSON: true},
            dataType: 'json',
            cache: false,
            timeout: 5000,
            success: function(data) {
                console.log(data); // for testing only but this should call a handler for the data  
            },
            complete: function(){
                checkMessagesTimeout = setTimeout(function(){
                    checkMessages();
                }, 1000);
            }
        });

    }

    $(window).load(function(){
        checkMessages();
    });
});

This version has the added benefit of storing the timeout's reference into a variable, so should the need arise, you can abort the timeout before the next ajax call is made, by calling clearTimeout(checkMessagesTimeout); 此版本具有将超时引用存储到变量中的额外好处,因此如果需要,您可以通过调用clearTimeout(checkMessagesTimeout);在下一次ajax调用之前中止超时clearTimeout(checkMessagesTimeout);

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

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