开发者

How can i make press event with Javascript (jquery, dojo)?

开发者 https://www.devze.com 2023-04-06 21:14 出处:网络
In the touch screen. if i keep press in the center of a screen for 20 seconds, i want to show alert(\"This is a secret window\");

In the touch screen.

  • if i keep press in the center of a screen for 20 seconds, i want to show alert("This is a secret window");

  • If i pressed for 5 seconds each corners in the touch screen as: top/left top/right bottom/right bottom/left it will show alert("This is more开发者_运维知识库 secret window");

How do i do this in jQuery/Dojo or Mootools or in plain Javascript? Any one ever did this? I do not find any "pressed" event with time set.

Note: there will be also many normal press inputs, so i want to do it in optimized way, for real-time actions except those two.


You need two event handlers and a timer.

// Put this lot in a closure so you don't pollute the global namespace.
(function () {
    var timer;
    function onTouchStart() {
        timer = setTimeout(doTheThing, 20*1000);
    }
    function onTouchEnd() {
        clearTimeout(timer);
    }
    function doTheThing() {
        alert('foo')
    }
})();

Bind onTouchStart/End to the appropriate events on the appropriate elements.

See a working example altered to operate with a mouse button and for 5 seconds (because 20 is too much time to hang around for this test).

0

精彩评论

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

关注公众号