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
2010-07-28 13:31:34
It's not using jquery as i can see.
antyrat
2010-07-28 13:32:57
@antyrat - A solution doesn't need to... Use jQuery if it helps, don't if it doesn't.
Nick Craver
2010-07-28 13:43:52
I agree with Nick's comment. Why use jQuery for a function that does not need it?
Liam Spencer
2010-07-28 13:46:34
@Nick Craver Ok, I've just noticed that QO asked jquery solution, so I supose it can be resolved with delay() jquery function.
antyrat
2010-07-28 13:46:49
Please don't use `window.onload` - either use some DOM-ready event or simply place the script below any referenced elements.
J-P
2010-07-28 13:47:49
@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
2010-07-28 13:48:10
@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
2010-07-28 13:49:09
@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
2010-07-28 13:52:57
@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
2010-07-28 13:57:36
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
2010-07-28 15:00:13
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
2010-07-29 07:10:21
+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
2010-07-28 13:43:15
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
2010-07-28 13:46:44
Thank you. It was helpful and I accept your soultion.Thanks @Nick for timely suggestions
Maju
2010-07-29 07:05:31