javascript delay
-
I use the following code in order to display a fake sending method that lasts 1 second:
$("#" + action + "-submit-message").text("sending...");
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + 1000);$("#" + action + "-submit-message").text("sent!");
Despite of that, I can see only the "sent!" message in the end of the function, when I expect to see the "sending..." for a second. What can be the problem?
-
I use the following code in order to display a fake sending method that lasts 1 second:
$("#" + action + "-submit-message").text("sending...");
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + 1000);$("#" + action + "-submit-message").text("sent!");
Despite of that, I can see only the "sent!" message in the end of the function, when I expect to see the "sending..." for a second. What can be the problem?
I guess your while loop is locking out the DOM update while it runs. Try using a timer instead:
$("#" + action + "-submit-message").text("sending...");
setTimeout(function() {
$("#" + action + "-submit-message").text("sent!");
},1000); -
I guess your while loop is locking out the DOM update while it runs. Try using a timer instead:
$("#" + action + "-submit-message").text("sending...");
setTimeout(function() {
$("#" + action + "-submit-message").text("sent!");
},1000);I have tried something similar to:
function f(){
$("#" + action + "-submit-message").text("sending...");setTimeout(done(),1000);
}
function done()
{
$("#" + action + "-submit-message").text("sent!");
}but what I get is the "sending..." message permanently(the "sent!" is never displayed)
-
I have tried something similar to:
function f(){
$("#" + action + "-submit-message").text("sending...");setTimeout(done(),1000);
}
function done()
{
$("#" + action + "-submit-message").text("sent!");
}but what I get is the "sending..." message permanently(the "sent!" is never displayed)
Try removing the () from inside the
setTimeout
call, like this:setTimeout(done,1000);
This means you pass the
done
function tosetTimeout
to call when the timer runs down, whereassetTimeout(done(),1000);
callsdone
at the start and passes the result tosetTimeout
to run later, which is not what you want. -
Try removing the () from inside the
setTimeout
call, like this:setTimeout(done,1000);
This means you pass the
done
function tosetTimeout
to call when the timer runs down, whereassetTimeout(done(),1000);
callsdone
at the start and passes the result tosetTimeout
to run later, which is not what you want. -
I think it works, but what if I have to pass parameters to a function? Is there a way to do it?
You should be able to pass parameters to your functions - but you might like to read up on Javascript closures first, or you could find things don't behave quite the way you expect: http://www.javascriptkit.com/javatutors/closures.shtml[^]
-
I have tried something similar to:
function f(){
$("#" + action + "-submit-message").text("sending...");setTimeout(done(),1000);
}
function done()
{
$("#" + action + "-submit-message").text("sent!");
}but what I get is the "sending..." message permanently(the "sent!" is never displayed)
Hi, Just inclose the done function in either double or single qouts like so:
setTimeout('done()', 1000);
that way you can even pass parameters in the call to the "done" function. Good luck, Morgs