开发者

Prototype (Javascript): How do I observe multiple events for the same element?

Veeery basic question, but I haven't been able to find a simple answer (and I have no idea what I'm doing and no time to properly learn javascript or prototype at the moment :-/).

I'm trying to re-use this bit of code and it would even do exactly what I want ;), except that I don't know how to change this part:

function init() {

    Event.observe(document.body, 'mousemove'开发者_StackOverflow, resetIdle, true);

    setIdle();

}

I'd like it to observe not only 'mousemove', but also 'keydown' at the same time. How do I go about this?


function init() {

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

    setIdle();

}

It's obvious, isn't it?


On the chance that you only want to reset the timer when moving the mouse and holding a key at the same time you'll need to update this function:

function resetIdle(event){

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜