getJSON timeout handling

后端 未结 6 903
借酒劲吻你
借酒劲吻你 2021-02-12 10:36

I am using jQuery getJSON() function. This function getting data with no problem. But sometimes waiting, waiting waiting... And my loading bar showing loading loadi

6条回答
  •  臣服心动
    2021-02-12 11:04

    As lethal-guitar mentioned getJSON() function is just an shorthand for $.ajax(). If you want to detect if a timeout has occurred rather than an actual error use the code below.

    var request = $.ajax({
        dataType: "json",
        url: url,
        data: data,
        success: function( ) { },
        timeout: 2000
    }).fail( function( xhr, status ) {
        if( status == "timeout" ) {
            // do stuff in case of timeout
        }
    });
    

提交回复
热议问题