jQuery的/ AJAX循环的setTimeout [英] Jquery/ajax Looping SetTimeout

查看:135
本文介绍了jQuery的/ AJAX循环的setTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是卡在我的setTimeout函数。我想要做的是循环我的setTimeout为我找回通话功能。我曾尝试对setInterval的,但使用的setInterval是badnews我的应用程序这就是为什么我切换到setTimeout的。但我似乎无法弄清楚如何使setTimeout的工作再其加载完成后..这里是我试过到目前为止,仍然试图使它在目前工作..

Hi I'm stuck on my setTimeout function. What i'm trying to do is to loop my setTimeout for my retrieve conversation function.. I have tried this on setInterval but using setInterval is a badnews for my application which is why I switch to setTimeout.. But I cant seem to figure out how to make the setTimeout work again after its done loading.. here is what I have tried so far and still trying to make it work at the moment..

JavaScript的:

Javascript:

id = setTimeout(function()
{
    $.ajax(
    {
        url: "includes/handlechat.php",
        type: "GET",
        data: data,
        dataType: 'json',
        success: function(result)
        {
            $("#clog").empty();
            $.each(result, function(rowKey, row) 
            {
                $("#clog")
                    .append('<p ><h4>'+ row.username +':</h4>' + row.message_content + '</p>' );
            });
        },
        complete: function () 
        { 
            clearTimeout(id);
        }
    })
}, 1101);

任何提示或建议?

Any tips or suggestion ?

推荐答案

把code的作用,并呼吁它的成功或完整的处理程序:

Put the code in a function and call it in the success or complete handler:

function load() {
    setTimeout(function () {
        $.ajax({
            url: "includes/handlechat.php",
            type: "GET",
            data: data,
            dataType: 'json',  
            success: function (result) {
                $("#clog").empty();
                $.each(result, function (rowKey, row) {
                    $("#clog").append('<p ><h4>' + row.username + ':</h4>' + row.message_content + '</p>'); 
                }); 
            },
            complete: load
        });
    }, 1101);
}
load();

您也可以使用的生活to避免创建在当前环境中的另一绑定:

You can also use an IIFE to avoid creating another binding in the current environment:

(function load() {
   // setTimeout here
}());

这篇关于jQuery的/ AJAX循环的setTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆