0

i have this simple load request:

$('#test').load('/sidebar/test/format/html').css("display","block");

the issue is that chrome is showing the request has 1.2 sec blooking.

i'm thinking of using $.when:

var test = $('#test');
$.when(test.load('/sidebar/test/format/html')).done(function(){
    test.css("display","block");
});

but this doesn't seem to work.

any ideas?

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337

2 Answers2

2

I believe what you want is to just use the callback as defined in the docs:

var test = $('#test');
test.load('/sidebar/test/format/html', function(){
    test.css('display','block');
})
Hamms
  • 5,016
  • 21
  • 28
  • 1
    One minor point of clarification; I believe you are slightly misunderstanding what "blocking" means. This function is non-blocking, meaning that any code put after it will be executed immediately. Because it's non-blocking and yet you want something to happen when the code finishes, you use what's called a callback. It's like you call load and say "okay buddy, you go off and do your thing and when you're done, do this other thing. Now that I've started you, I'm just going to move on and do the rest of my code and trust that you'll call your callback when you're done." Does that make sense? – Hamms Mar 25 '13 at 19:55
1

For whatever reason .load does not implement Deferred, so you can't chain the .done method to it. You don't need to use .load in this situation, though.

$.get('/sidebar/test/format/html').done(function (html) {
    $("#test").html(html).show();
});
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405