document.onclick settimeout function javascript help
- by Jamex
Hi,
I have a document.onclick function that I would like to have a delay. I can't seem to get the syntax right.
my original code is
<script type="text/javascript">
document.onclick=check;
function check(e){do something}
I tried the below, but that code is incorrect, the function did not execute and nothing happened.
<script type="text/javascript">
document.onclick=setTimeout("check", 1000);
function check(e){do something}
I tried the next set, the function got executed, but no delay.
<script type="text/javascript">
setTimeout(document.onclick=check, 1000);
function check(e){do something}
what is the correct syntax for this code.
TIA
Edit:
The solutions were all good, my problem was that I use the function check to obtain the id of the element being clicked on. But after the delay, there is no "memory" of what was being clicked on, so the rest of the function does not get executed. Jimr wrote the short code to preserve clicked event.
The code that is working is
// Delay execution of event handler function "f" by "time" ms.
document.onclick = makeDelayedHandler(check, 250);
function makeDelayedHandler( f, time)
{
return function( e )
{setTimeout(function()
{f( e );}, time ); };
}
function check(e){
var click = (e && e.target) || (event && event.srcElement);
.
.
.
Thank you all.