万一发生超时错误,如何再次进行Ajax调用 [英] How to make the Ajax call again in case Time out error occurs

查看:267
本文介绍了万一发生超时错误,如何再次进行Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html文件中有一个文档准备功能,如下所示

I have a document ready function inside my html file as shown below

$(document).ready(function() 
{
   cust_id = getParameterByName('customer_id');

    var locationAjaxCall = $.ajax({
        type: 'GET',
        url: url+'/OMS/oms1/getlocationscreen?screen_ids=' + screen_ids,
        jsonpCallback: 'jsonCallback',
        cache: true,
        dataType: 'jsonp',
        jsonp: false,
        success: function (response) {

        },
        error: function (xhr, ajaxOptions, thrownError) {
        }
    });
    locationAjaxCall.done(function() {
        $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            jsonp: false,
             timeout: 5000,
            success: function(response) {
            },
             error: function(x, t, m) {
            if(t==="timeout") {
            alert("got timeout");
            }
    }
});
}).done(function() {
    });
});

第二个Ajax调用.我将超时保持5秒钟,如下所示

For the second Ajax call . i am keeping a timeout for 5 seconds as shown below

我的问题是,如果第二个Ajax调用期间发生超时,我该如何再次进行该Ajax调用?

My question is in case a timeout occurs during the second Ajax call , how can i make that Ajax call again ??

(我不想重新加载整个页面,因为我可能会丢失一些已经设置的数据)

(I don't want to reload the entire page as i might lose some data which was already set )

请让我知道它是否可能??

Please let me know if its possible ??

推荐答案

您的意思是这样吗?使用setInterval并在间隔成功时清除间隔.

You mean like this? Using setInterval and clear the interval when its successful.

  $(document).ready(function() 
    {
       cust_id = getParameterByName('customer_id');

        var locationAjaxCall = $.ajax({
            type: 'GET',
            url: url+'/OMS/oms1/getlocationscreen?screen_ids=' + screen_ids,
            jsonpCallback: 'jsonCallback',
            cache: true,
            dataType: 'jsonp',
            jsonp: false,
            success: function (response) {

            },
            error: function (xhr, ajaxOptions, thrownError) {
            }
        });
        locationAjaxCall.done(function() {
            var intvl=setInterval(function(){
            $.ajax({
                type: 'GET',
                url: url+'/OMS/oms1/fromscratchmodified?screen_ids=' + screen_ids,
                jsonpCallback: 'jsonCallback',
                dataType: 'jsonp',
                jsonp: false,
                 timeout: 5000,
                success: function(response) {
                 clearInterval(intvl);
                },
                 error: function(x, t, m) {
                if(t==="timeout") {
                alert("got timeout");
                }
              }
          });
        },5000);
      }).done(function() {
     });
    });

这篇关于万一发生超时错误,如何再次进行Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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