如何将一些指定的时间设置为ajax调用,这样如果请求正在花费时间检索数据,它应该来到FAIL块

How to set the some specified time to ajax call ,So that if request is taking time retrieve the data ,It should come to the FAIL Block

本文关键字:费时间 检索 数据 请求 FAIL 如果 时间 调用 ajax 设置      更新时间:2023-09-26
var ajaxRequest = new enyo.Ajax({
    cacheBust: false,
    contentType: 'application/json;charset=utf-8',
    method: 'POST',
    timeout: 8000,
    async: false,
    handleAs: 'json',
    data: JSON.stringify({
        // Data to connect to the external service.
        url: url
            method: 'GET',
        contenttype: 'application/json;charset=utf-8',
        content: 'username=l&pwd=p' + searchParams
    }),
    success: function (inSender, inResponse) {
    },
    fail: function (inSender, inResponse) {
    }
    ajaxRequest.go(ajaxRequest.data).response('success').error('fail');
};

比方说,调用网络服务需要5到6秒的时间,或者互联网连接速度慢,如何重定向到失败块

手动调用fail-callback-func怎么样,如下所示:

var getDataFail = true;
    function getData() {
        setTimeOut(fileCallbackFunc, 6000);
        $.ajax({
            url: yourURL,
            success: doneCallbck,
            error: doneCallbck,
            //other ajax params
        });
    }

    function fileCallbck() {
        if (getDataFail) {
            //your fail logic...
            console.error('get data fail');
        }
    }
    function doneCallbck() {
        getDataFail = false;
        console.error('get data done');
    }