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 Jul 28, 2010 at 13:29
Maju
1,2052 gold badges11 silver badges17 bronze badges
-
possible duplicate of : stackoverflow.com/questions/121817/…mint– mint2010年07月28日 13:30:58 +00:00Commented Jul 28, 2010 at 13:30
-
use setTimeout to update the div's textNazmul– Nazmul2010年07月28日 13:35:47 +00:00Commented Jul 28, 2010 at 13:35
-
@monO: That question does not mention jQuery or a random delay, I think.Peter Jaric– Peter Jaric2010年07月28日 13:36:10 +00:00Commented Jul 28, 2010 at 13:36
-
@peter: True, definitly not a duplicate; I'll leave the link just for reference though!mint– mint2010年07月28日 13:51:47 +00:00Commented Jul 28, 2010 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();
});
answered Jul 28, 2010 at 13:43
James
112k32 gold badges165 silver badges177 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Nick Craver
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(); });Maju
Thank you. It was helpful and I accept your soultion.Thanks @Nick for timely suggestions
<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.8k14 gold badges69 silver badges96 bronze badges
answered Jul 28, 2010 at 13:31
Liam Spencer
9361 gold badge11 silver badges26 bronze badges
11 Comments
antyrat
It's not using jquery as i can see.
Nick Craver
@antyrat - A solution doesn't need to... Use jQuery if it helps, don't if it doesn't.
James
Please don't use
window.onload - either use some DOM-ready event or simply place the script below any referenced elements.James
@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.
MooGoo
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!?" |
in jQuery, it would be:
$("myDiv").html("new content");
answered Jul 28, 2010 at 13:33
Dave Thieben
5,4462 gold badges31 silver badges40 bronze badges
Comments
lang-js