使用clearTimeout取消超时事件 [英] Using clearTimeout to cancel a timeout event

查看:82
本文介绍了使用clearTimeout取消超时事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但明确的超时不起作用,我不明白为什么,有没有人有任何想法? (使用Prototype框架)

I have the following code but the clear timeout doesn't work and I can't understand why, does anyone have any ideas? (Using the Prototype framework)

谢谢

function foo() {
    $("navigation").observe('mouseover',
        function (event) {
            clearTimeout(bar);
        }
    ).observe('mouseout',
        function (event) {
            setTimeout(bar, 1000);
        }
    );
}

function bar() {
    alert("hi");
}


推荐答案

您需要存储结果在变量中使用 setTimeout ,并使用 clearTimeout 清除该变量,而不是函数:

You need to store the result of setTimeout in a variable, and use clearTimeout to clear that variable, not the function:

var timer;

function foo() {
    $("navigation").observe('mouseover',
        function (event) {
                clearTimeout(timer);
        }
    ).observe('mouseout',
        function (event) {
                timer = setTimeout(bar, 1000);
        }
    );
}

function bar() {
    alert("hi");
}

这篇关于使用clearTimeout取消超时事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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