views:

77

answers:

3

Hi I need to replace a text within div that says "Please wait..." with "Ok.Its done." after a random delay.Please help.Thanks in advance.

+2  A: 
<script type="text/javascript"> 
window.onload = function () { 
  setTimeout(function () { 
    var div = document.getElementById('yourDiv'); 
    div.innerHTML = "OK. It's done.";  
  }, 10000); 
} 
</script> 
Liam Spencer
It's not using jquery as i can see.
antyrat
@antyrat - A solution doesn't need to... Use jQuery if it helps, don't if it doesn't.
Nick Craver
I agree with Nick's comment. Why use jQuery for a function that does not need it?
Liam Spencer
@Nick Craver Ok, I've just noticed that QO asked jquery solution, so I supose it can be resolved with delay() jquery function.
antyrat
Please don't use `window.onload` - either use some DOM-ready event or simply place the script below any referenced elements.
J-P
@antyrat - That's tremendous overkill IMO, and not at all what `.delay()` was designed for. If you need complex queuing go for it...you you need a simple delay, use `setTimeout()` (which `.delay()` is just a wrapper for).
Nick Craver
@Liam, that argument could be applied to the whole of jQuery... It's not about whether it can be done with plain JS (which is, essentially, all jQuery is) -- it's a question of terseness, readability and cross-browser compatibility.
J-P
@J-P - That applies here though doesn't it? It can be done with less code *without* jQuery: `setTimeout(function () { document.getElementById('yourDiv').innerHTML = "OK. It's done."; }, 10000);` Yes the selectors can shorten it, but the premise of using `setTimeout()` vs `.delay()` in this case is absurd to me, it's a drastic over-complication.
Nick Craver
@Nick, I wouldn't say it's drastic. We really don't know much because the OP hasn't said much. We don't know that the div has an ID, we don't know if the OP is already using jQuery for other tasks etc. Depending on the situation I might agree with you but given that we simply don't know I thought it best to answer the question in jQuery. If the question was not tagged with "jQuery" I would have gone for a plain JS approach too.
J-P
If you wanted you could still use jQuery within the `setTimeout` function, as in `var div = $('#yourDiv').text("Ok. It's done.");`. But there is no good reason to use jQuery for a simple for a simple delay. To me that is tantamount to saying "Why use `var x=5;` when you could write `$.extend(variables, {x: 5});` in jQuery!?"
MooGoo
Thank you all for the support. I could use both solutions js for mockup version and jQuery for the production version.Thank u all.
Maju
A: 

in jQuery, it would be:

$("myDiv").html("new content");
dave thieben
+3  A: 

Try this:

$("#foo").text("Please Wait...")
         .delay(Math.random() * 1000) // between 0 and 1000 milliseconds
         .queue(function(q){
             $(this).text("okay, it's done");
             q();
         });
J-P
You should call the next function from any queue function or the queue for this element is forever stuck, like this: `.queue(function(n){ $(this).text("okay, it's done"); n(); });`
Nick Craver
@Nick, sorry. Forgot. Thanks for reminding me.
J-P
Thank you. It was helpful and I accept your soultion.Thanks @Nick for timely suggestions
Maju