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.
Asked
Active
Viewed 1,406 times
0
-
possible duplicate of : http://stackoverflow.com/questions/121817/replace-text-inside-a-div-element – mint Jul 28 '10 at 13:30
-
use setTimeout to update the div's text – Nazmul Jul 28 '10 at 13:35
-
@monO: That question does not mention jQuery or a random delay, I think. – Peter Jaric Jul 28 '10 at 13:36
-
@peter: True, definitly not a duplicate; I'll leave the link just for reference though! – mint Jul 28 '10 at 13:51
3 Answers
3
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();
});

James
- 109,676
- 31
- 162
- 175
-
1You 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 Jul 28 '10 at 13:46
-
Thank you. It was helpful and I accept your soultion.Thanks @Nick for timely suggestions – Maju Jul 29 '10 at 07:05
2
<script type="text/javascript">
window.onload = function () {
setTimeout(function () {
var div = document.getElementById('yourDiv');
div.innerHTML = "OK. It's done.";
}, 10000);
}
</script>

Ben Everard
- 13,652
- 14
- 67
- 96

Liam Spencer
- 936
- 1
- 11
- 25
-
-
@antyrat - A solution doesn't need to... Use jQuery if it helps, don't if it doesn't. – Nick Craver Jul 28 '10 at 13:43
-
I agree with Nick's comment. Why use jQuery for a function that does not need it? – Liam Spencer Jul 28 '10 at 13:46
-
@Nick Craver Ok, I've just noticed that QO asked jquery solution, so I supose it can be resolved with delay() jquery function. – antyrat Jul 28 '10 at 13:46
-
1Please don't use `window.onload` - either use some DOM-ready event or simply place the script below any referenced elements. – James Jul 28 '10 at 13:47
-
@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 Jul 28 '10 at 13:48
-
@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. – James Jul 28 '10 at 13:49
-
@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 Jul 28 '10 at 13:52
-
1@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. – James Jul 28 '10 at 13:57
-
1If 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 Jul 28 '10 at 15:00
-
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 Jul 29 '10 at 07:10