document.onclick settimeout 函数 javascript 帮助 [英] document.onclick settimeout function javascript help

查看:41
本文介绍了document.onclick settimeout 函数 javascript 帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要延迟的 document.onclick 函数.我似乎无法正确使用语法.

I have a document.onclick function that I would like to have a delay. I can't seem to get the syntax right.

我的原始代码是

<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

解决方案都很好,我的问题是我使用函数检查来获取被点击的元素的id.但是在延迟之后,没有被点击的记忆",所以函数的其余部分不会被执行.Jimr 编写了简短的代码来保存点击事件.

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.

正在运行的代码(不适用于 IE6)

The code that is working (not work in IE6)

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 && e.target) || (event && event.srcElement);  
.
.
.

谢谢大家.

更新:kennebec 的解决方案适用于 IE6.

update: kennebec's solution works for IE6.

推荐答案

window.twotimer=function(e){
    if(arguments[1]!= 'timer'){
        // If the 'timer' argument was not passed,
        // the function was called from the event,
        // so call it again with a timer

        e= window.event || e;
        var target= e.target || e.srcElement;
        setTimeout(function(){return twotimer(target,'timer')},1000);

        if(e.stopPropagation) e.stopPropagation();
        e.cancelBubble=true;
        return false;
    }
    // if you get to this point, e is the element node clicked
    // a second ago-
    // put your function body here, using e for the element clicked
    alert(e.nodeName+' id='+ e.getAttribute('id')+'\nwas clicked a second ago');
}
document.onclick= twotimer;

这篇关于document.onclick settimeout 函数 javascript 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆