原型(Javascript):如何观察同一元素的多个事件?

时间:2011-02-19 12:45:57

标签: javascript prototypejs

Veeery基本问题,但我找不到一个简单的答案(我不知道我在做什么,现在没时间正确学习javascript或原型: - /)。

我正在尝试重新使用this bit of code,它甚至可以完全按照我的意愿行事;),除了我不知道如何改变这一部分:

function init() {

    Event.observe(document.body, 'mousemove', resetIdle, true);

    setIdle();

}

我不仅要观察'mousemove',还要同时观察'keydown'。我该怎么做?

2 个答案:

答案 0 :(得分:1)

function init() {

    Event.observe(document.body, 'mousemove', resetIdle, true);
    Event.observe(document.body, 'keydown', resetIdle, true);

    setIdle();

}

很明显,不是吗?

答案 1 :(得分:1)

如果您只想在移动鼠标并同时按住键时重置计时器,则需要更新此功能:

function resetIdle(event){

    if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
        window.clearTimeout( timeOut );
        setIdle();
    }

}