I have a document.onclick function that I would like to have a delay. I can't seem to get the syntax right.
我有一个文档。onclick函数我想要延迟。我似乎不太懂语法。
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.
解决方案都很好,我的问题是我使用函数检查来获得被点击元素的id。但是在延迟之后,没有“内存”被点击,因此函数的其余部分不会被执行。Jimr编写了简短的代码来保存单击事件。
The code that is working (not work in IE6)
正在工作的代码(不在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);
.
.
.
Thank you all.
谢谢大家。
update: kennebec's solution works for IE6.
更新:kennebec的解决方案适用于IE6。
5 个解决方案
#1
1
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;
#2
4
Something like:
喜欢的东西:
document.onclick = function () {
setTimeout(check, 1000);
};
- The
setTimeout
method doesn't return a function, it returns a number, which is the timer Id that you can use in case you want to cancel the timer before it fires (withclearTimeout
) - setTimeout方法不返回一个函数,它返回一个数字,这是您可以使用的定时器Id,如果您想在它触发之前取消计时器(使用clearTimeout)
- You don't need to use strings as the first argument, use a function reference.
- 您不需要使用字符串作为第一个参数,使用函数引用。
#3
2
You can make a generic function that will make a delayed event handler. E.g.
您可以创建一个通用函数,它将生成一个延迟事件处理程序。如。
// 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 )
{
// Your original handler
}
document.onclick = makeDelayedHandler( check, 1000 );
#4
0
document.onclick = function() {
setTimeout("check()",1000);
};
function check() {
alert("I'm baaaaaack!");
}
That should work...
这应该工作……
#5
0
You need to call window.setTimeout()
.
您需要调用window.setTimeout()。
Also, window.setTimeout()
takes a function reference so no need for quotes around check
. Adding quotes does an eval()
on the quoted string, which is slow and unnecessary.
另外,window.setTimeout()函数引用了函数引用,因此无需在检查时引用引号。添加引号在引用的字符串上执行eval(),这是缓慢且不必要的。
This should work
这应该工作
document.onclick = function(e) {
function check() {
return function() {
//do something
}
}
window.setTimeout(check, 1000);
}
#1
1
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;
#2
4
Something like:
喜欢的东西:
document.onclick = function () {
setTimeout(check, 1000);
};
- The
setTimeout
method doesn't return a function, it returns a number, which is the timer Id that you can use in case you want to cancel the timer before it fires (withclearTimeout
) - setTimeout方法不返回一个函数,它返回一个数字,这是您可以使用的定时器Id,如果您想在它触发之前取消计时器(使用clearTimeout)
- You don't need to use strings as the first argument, use a function reference.
- 您不需要使用字符串作为第一个参数,使用函数引用。
#3
2
You can make a generic function that will make a delayed event handler. E.g.
您可以创建一个通用函数,它将生成一个延迟事件处理程序。如。
// 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 )
{
// Your original handler
}
document.onclick = makeDelayedHandler( check, 1000 );
#4
0
document.onclick = function() {
setTimeout("check()",1000);
};
function check() {
alert("I'm baaaaaack!");
}
That should work...
这应该工作……
#5
0
You need to call window.setTimeout()
.
您需要调用window.setTimeout()。
Also, window.setTimeout()
takes a function reference so no need for quotes around check
. Adding quotes does an eval()
on the quoted string, which is slow and unnecessary.
另外,window.setTimeout()函数引用了函数引用,因此无需在检查时引用引号。添加引号在引用的字符串上执行eval(),这是缓慢且不必要的。
This should work
这应该工作
document.onclick = function(e) {
function check() {
return function() {
//do something
}
}
window.setTimeout(check, 1000);
}