0

For example, i want to do know if $('#seomDiv').load(...); is completed in real time. I'm referring to the ajax .load() function.

Any idea?

EDIT: hmmmmm... I'm guessing something with setInterval()!

EDIT #2: I'm wondering if the completiong of the event can be somehow detected in real time from a totally seperate queue.

EDIT #3: I guess I have a better question: What event gets triggered when a jQuery animation or ajax is finished??

trusktr
  • 44,284
  • 53
  • 191
  • 263
  • Why do you want to check continuously whether something is loaded, when you can be *notified* that something is loaded? It is still "real time". – Felix Kling Mar 06 '11 at 10:42
  • True. How do you send a notification to another (asynchronous?) function? – trusktr Mar 06 '11 at 10:45

3 Answers3

5

(Assuming you mean the Ajax .load() function)

Pass a callback to .load():

$('#someDiv').load("someurl.html", function(){
    alert('completed');
});

Documentation

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks for the help. Is there a way to determine if the .load() is completed in real time without using a call back? Or, how could I use a callback to trigger an event that lets me do something else, elsewhere... For example, using next() with queues. hehe – trusktr Mar 06 '11 at 10:32
  • 1
    @trusktr: As Ajax loads asynchronously, the only way is the callback. You can put whatever you want in the callback. Inside the callback you also have access to variables defined in the parent scope (as it is a closure). You can do: `function(next){ $(...).load(..., function(){ next();})};` – Felix Kling Mar 06 '11 at 10:39
  • Is one able to run setInterval() and a jQuery animation asynchronously? – trusktr Mar 06 '11 at 10:41
  • 2
    @trusktr: I think before you think about possible solutions, you should decribe your problem better. The animations use `setTimeout` themselves. You have to be careful when mixing all of these. – Felix Kling Mar 06 '11 at 10:44
  • I'm really wondering if there's a way to detect completion from within ANOTHER seperate queue. – trusktr Mar 16 '11 at 00:31
  • have you tried async:false option ? it's performs synchronized ajax call. – ertan Mar 16 '11 at 00:57
  • 1
    @trusktr: Not sure what you mean with *ANOTHER seperate queue*, but you can listen globally for any Ajax request via `.ajaxComplete()` (http://api.jquery.com/ajaxComplete/) – Felix Kling Mar 16 '11 at 07:49
  • @Felix... I think that may be the answer! – trusktr Mar 16 '11 at 08:55
  • Thanks for the idea. What I was looking was a way to detect when the animation of a .load() method was completed from an external function in real time. See my answer for the solution. – trusktr Sep 03 '11 at 12:15
1

jQuery .size() will tell you if an element exists, therefore has been loaded in the dom.

if ($('#seomDiv').size()){
 //code here
}

Non jQuery solution: You can also use javascripts native object property length:

if(document.getElementById("someDiv").length){
 //code here
}
amosrivera
  • 26,114
  • 9
  • 67
  • 76
  • Thanks for the idea. What I was looking was a way to detect when the animation of a .load() method was completed from an external function. See my answer for the solution. – trusktr Sep 03 '11 at 12:16
0

The solution is to set a boolean from the .load() callback. An external function can check the value of the boolean in intervals. Check here for the details: How do you make an event listener that detects if a boolean variable becomes true?

Community
  • 1
  • 1
trusktr
  • 44,284
  • 53
  • 191
  • 263
  • A better solution would be to trigger a custom event from the success callback and let your other code listen to this event. – Felix Kling Sep 03 '11 at 12:34