views:

55

answers:

3

Hi,

Is it possible to check if the document is not ready and execute a function periodically in jQuery?

Simply I would like to achieve something like :

$('document').isNotReady(function(){

    $('#divState').text('Still Loading');  
});

$('document').Ready(function(){

    $('#divState').text('Loaded');
});

Is there a built-in function in jQuery to achieve something like this?

+5  A: 
rahul
+! just think the other way round!
Andreas Niedermair
A: 

Not as far as I am aware. You could easily immidate it though:

var bIsReady = false;

$('document').Ready(function(){ 
    bIsReady = true;
    $('divState').text('Loaded'); 
}); 

/*
    use bIsReady somehow
/*

But I'm not sure why you would want to when you could just set it in the HTML:

<div>Still Loading</div>

And then change it as per your code above when the document is ready.

Also, if there was some NotReady function, you couldn't guarantee that the DIV you were setting was actually ready.

Also, you are selecting an element called DivState. Do you maybe mean: $('#DivState') ?

James Wiseman
+1  A: 

HTML part:

<input type="button" onclick="example_ajax_request()" value="Click Me!" />
<div id="example-placeholder">
  <p>Placeholding text</p>
</div>

jQuery part:

function example_ajax_request() {
  $('#example-placeholder').html('<p><img src="/images/ajax-loader.gif" width="220" height="19" /></p>');
  $('#example-placeholder').load("/examples/ajax-loaded.html");
}

Source: http://www.electrictoolbox.com/load-content-jquery-ajax-loading-image/

Ondrej Slinták