Note that there are some explanatory texts on larger screens.

plurals
  1. POdocument.onclick settimeout function javascript help
    primarykey
    data
    text
    <p>I have a document.onclick function that I would like to have a delay. I can't seem to get the syntax right.</p> <p>my original code is </p> <pre><code>&lt;script type="text/javascript"&gt; document.onclick=check; function check(e){do something} </code></pre> <p>I tried the below, but that code is incorrect, the function did not execute and nothing happened.</p> <pre><code>&lt;script type="text/javascript"&gt; document.onclick=setTimeout("check", 1000); function check(e){do something} </code></pre> <p>I tried the next set, the function got executed, but no delay.</p> <pre><code>&lt;script type="text/javascript"&gt; setTimeout(document.onclick=check, 1000); function check(e){do something} </code></pre> <p>what is the correct syntax for this code.</p> <p>TIA</p> <p>Edit:</p> <p>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.</p> <p>The code that is working (not work in IE6)</p> <pre><code>document.onclick = makeDelayedHandler( check, 1000 ); // Delay execution of event handler function "f" by "time" ms. function makeDelayedHandler( f, time) { return function( e ) { var ev = e || window.event; setTimeout( function() { f( ev ); }, time ); }; } function check(e){ var click = (e &amp;&amp; e.target) || (event &amp;&amp; event.srcElement); . . . </code></pre> <p>Thank you all.</p> <p>update: kennebec's solution works for IE6.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload