document.onclick settimeout function javascript help

Posted by Jamex on Stack Overflow See other posts from Stack Overflow or by Jamex
Published on 2010-06-15T18:32:20Z Indexed on 2010/06/16 0:42 UTC
Read the original article Hit count: 267

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about settimeout