Question:
Pausing Javascript, waiting for User input.?
Birdy
2010-12-06 09:56:50 UTC
So simple question. You have this function

function f_name () {

showMessage("This is the first message");

showMessage("This message shows after the first is closed");

showMessage("This is the last message to be shown");

}

So the idea of this function is to show a div with the message in it with each showMessage(). However, I would like the function to pause, waiting for the user to click somewhere. The user would see the first message, click, then second message, click, and then the last message. Click. Stop.

Any ideas?
Three answers:
Namibnat
2010-12-06 11:32:05 UTC
Simply set a counter as a global variable (before the function call) and then iterate through an array with your messages in.



This is a very simple version of how you could do it:

















No message










JimDandy
2010-12-06 18:32:36 UTC
You cannot really pause Javascript except by using an alert or confirm function. You could create a pause by creating a loop for a period of time, waiting for something to happen, but that's nasty code.



Probably a better solution would be to let the processing finish after displaying the first message and creating an onclick event which recognizes when the first message is cleared to show the second, and so on for additional messages. Try something like this:



http://px1.us?37J
?
2010-12-06 18:03:46 UTC
alert() will pause JS. Otherwise, I think you will have to hook a function to the button clicks. The function replaces the div contents with new text. So it doesn't actually pause - it's event-driven.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...