getJSON timeout handling

后端 未结 6 1095
抹茶落季
抹茶落季 2021-02-12 10:25

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:20

    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
        }
    });
    

提交回复
热议问题