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.
Written by Stevo Perisic
Related protips
2 Responses
$.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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#