Time delay on click
37,238
Solution 1
This is the Javascript:
function myfunction() {
alert( "delayed" );
}
var delay = 1000;
setTimeout( myfunction, delay )
That's the essence. Now you need to hook it into a button on a html page: embed the function definition in < script >
... < /script >
tags, and schedule it in the onClick method.
....
<button onClick="setTimeout( myfunction, delay );">Click_And_Wait</button>
Solution 2
If you need to pass parameters:
function clickHandler(id,value,delay)
{
setTimeout( function() { actualFunction(id, value); }, delay );
}
function actualFunction(id, value)
{
alert( 'button ' + id + ' has value = ' + value );
}
<input id='clickButton' type='button'
onclick='clickHandler(this.id,this.value,5000);'
value='Click Me' />
Solution 3
Try the setTimeout function:
setTimeout(functionToExecute, 1000)
The first parameter is the function that will be triggered and the second is the delay in milliseconds.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 20, 2022Comments
-
Admin almost 2 years
Can anyone send me JavaScript code on how I can construct a time delay from the time I click a button on a page to the time the function called by the button click is executed. I am a novice with JavaScript, and I have some code that performs a function when I click a button, and I just want to have a time delay.