user2058775
user2058775

Reputation:

Creating a prompt that has a time delay?

I'm trying to create a prompt that has a time delay, the value that is written in the prompt is then used in other areas of the form. I have written some javascript coding but I believe there is a minor thing that i am doing wrong as currently the prompt and delay are working, but because the setTimeout function is being used, that is what is being displayed in the form, instead of the content of the prompt. This is my Javascript?

var name = setTimeout(function(){ prompt("What is your name?", "Type your full name here")},750); 
document.write("Document Written By: " + name + " (" + day + "/" + month + "/" + year + ") ")

Upvotes: 0

Views: 1836

Answers (1)

Matt Ball
Matt Ball

Reputation: 360066

If it depends on the value, and a function is asynchronous, you've got do it in the callback. Just like every other asynchronous piece of JavaScript...

setTimeout(function(){
    name = prompt("What is your name?", "Type your full name here");
    document.write("Document Written By: " + name + " (" + day + "/" + month + "/" + year + ") ");
},750);

But as @Jon commented, please do not use document.write.

Upvotes: 4

Related Questions