Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getJSON timeout handling

Tags:

I am using jQuery getJSON() function. This function getting data with no problem. But sometimes waiting, waiting waiting... And my loading bar showing loading loading loadin at center of page. So jQuery ajax() function have an timeout variable. But i want to use getJSON function. And i think that i can use ajaxStart() and ajaxStop() functions. But how?

$('.loadingDiv')     .hide()     .ajaxStart(function() {         $(this).fadeIn();         setTimeout("throw '';",15000) //i used this but didn't work         setTimeout("return;",15000) //i used this but didn't work         setTimeout("abort();",15000) //i used this but didn't work.(Abort all ajax events)     })     .ajaxStop(function() {         $(this).fadeOut();     }); 
like image 319
pheaselegen Avatar asked Jan 09 '13 15:01

pheaselegen


1 Answers

getJSON() returns a promise on which you can call the abort function :

var p = $.getJSON(..., function(){ alert('success');}); setTimeout(function(){ p.abort(); }, 2000); 

EDIT : but if your goal is just to abort if it takes too much time, then lethal-guitar's answer is better.

like image 66
Denys Séguret Avatar answered Sep 25 '22 02:09

Denys Séguret