Last Updated: December 26, 2018
·
789
· stevoperisic

when loading JSON

When loading JSON use the jQuery .promise(), like so:

var object = {};

$.ajax({
url: 'someURL.php/someparam'
sucess: function(data){
$.each(data, function(key, val){
object.push(val)
})
}
}).promise().done( function(){
do something with object
} );

This eliminates setting timeouts or having lag while the JSON loads, it acts like a callback to the $.ajax

Hope this helps, it sure helped me recently.

2 Responses
Add your response

$.ajax returns a jQuery XMLHttpRequest (jqXHR) object which wraps the $.promise object and passes through functions to it. So you can actually call the .done method directly on the $.ajax return.

over 1 year ago ·

Thank you.

over 1 year ago ·