Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果连接丢失,javascript不会重新连接到服务器端_Javascript_Jquery_Ajax - Fatal编程技术网

如果连接丢失,javascript不会重新连接到服务器端

如果连接丢失,javascript不会重新连接到服务器端,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个连接到服务器的Jquery+Ajax页面,它获取一些数据并显示出来。这是一直在做的,函数在完成所有任务并开始一次又一次地执行之后调用自己。但如果komputer失去互联网连接,一切都会停止。当连接再次出现时,什么也没有发生。下面是javascript代码。我如何才能批准它,以便在重新连接后继续工作 $(document).ready(function() { var a=1; var max='23'; function kiosk() { if(a<max)

我有一个连接到服务器的Jquery+Ajax页面,它获取一些数据并显示出来。这是一直在做的,函数在完成所有任务并开始一次又一次地执行之后调用自己。但如果komputer失去互联网连接,一切都会停止。当连接再次出现时,什么也没有发生。下面是javascript代码。我如何才能批准它,以便在重新连接后继续工作

$(document).ready(function() {
var a=1;
var max='23';

function kiosk() 
{
    if(a<max) 
    {
        $.post('engine.php', { index: a },
        function(result) 
        {
            result = jQuery.parseJSON(result);

            $('#main').css({'display':'none'});


            $('#div_city').html(result.dest);
            $('#div_price').html(result.price);
            $('#div_price_pre').html('From&nbsp;');
            $('#div_price_after').html('&nbsp;Euro');

                $('#main').fadeIn(2000).delay(3000).fadeOut(2000, function() 
                    {

                            $('#main').hide;
                            a++;
                            kiosk();


                    });

        });
    }
    else
    {
        a=1;
        kiosk();
    }

}
kiosk();
});
$(文档).ready(函数(){
var a=1;
var max='23';
功能信息亭()
{

如果(a脚本中断,因为我认为它会引发异常。您可以添加代码try-and-catch块,即使有错误,也可以继续尝试

try {
    result = jQuery.parseJSON(result);
    // do your work
} catch {
    // call function again
}

或者您可以使用jquery ajax,它有一个onerror事件。

使用
jquery.post
传递一个回调,该回调仅在成功时执行。 您应该使用具有不同回调的
jQuery.ajax
。请参阅使用
complete
success
error
回调


您甚至可以使用
statusCode
将特定的HTTP代码映射到自定义函数。

我建议您不要使用$

$.ajax({type:“post”,成功:function1(),失败:function2()})

在function2()中,您可以在超时后再次调用koisk。成功的函数将是您现在创建的函数

此链接将帮助您理解ajax功能

例如代码片段:

function kiosk() 
{
if(a<max) 
{
    $.ajax({type : "POST" , url : 'engine.php',data: { index: a },
    success : function(result) 
    {
        result = jQuery.parseJSON(result);

        $('#main').css({'display':'none'});


        $('#div_city').html(result.dest);
        $('#div_price').html(result.price);
        $('#div_price_pre').html('From&nbsp;');
        $('#div_price_after').html('&nbsp;Euro');

            $('#main').fadeIn(2000).delay(3000).fadeOut(2000, function() 
                {

                        $('#main').hide;
                        a++;
                        kiosk();


                });

    },error : function (xhr, ajaxOptions, thrownError){ setTimeout(200,kiosk())  }

    });
}
else
{
    a=1;
    kiosk();
}
功能亭()
{
如果(a)