简体   繁体   English

需要用于按住按钮的 javascript 代码

[英]Need javascript code for button press and hold

I'd like a short smallest possible javascript routine that when a mousedown occurs on a button it first responds just like a mouseclick and then if the user keeps the button pressed it responds as if the user was continously sending mouseclicks and after a while with the button held down acts as if the user was accelerating their mouseclicks...basically think of it like a keypress repeat with acceleration in time.我想要一个尽可能短的 javascript 例程,当在按钮上发生鼠标按下时,它首先像鼠标单击一样做出响应,然后如果用户保持按下按钮,它的响应就像用户连续发送鼠标点击一样,一段时间后按下按钮就像用户在加速他们的鼠标点击......基本上把它想象成随着时间的加速而重复按键。
ie user holds down mouse button (x=call function) - x___x___x___x__x__x_x_x_x_xxxxxxx即用户按住鼠标按钮(x=调用函数) - x___x___x___x__x__x_x_x_x_xxxxxxx

function holdit(btn, action, start, speedup) {
    var t;

    var repeat = function () {
        action();
        t = setTimeout(repeat, start);
        start = start / speedup;
    }

    btn.mousedown = function() {
        repeat();
    }

    btn.mouseup = function () {
        clearTimeout(t);
    }
};

/* to use */
holdit(btn, function () { }, 1000, 2); /* x..1000ms..x..500ms..x..250ms..x */

When the button is pressed, call window.setTimeout with your intended time and the function x , and set the timer again at the end of x but this time with a smaller interval. 当按下按钮,拨打window.setTimeout与你预期的时间和功能的x ,并在结束时再次设置定时器x ,但这次用较小的区间。

Clear the timeout using window.clearTimeout upon release of the mouse button. 在释放鼠标按钮时使用window.clearTimeout清除超时。

Just put the below toggleOn in the OnMouseDown and toggleOff in the OnMouseUp of the button. 只需在OnMouseDown中放置以下toggleOn,并在按钮的OnMouseUp中放置toggleOff。

var tid = 0;
var speed = 100;

function toggleOn(){
    if(tid==0){
        tid=setInterval('ThingToDo()',speed);
    }
}
function toggleOff(){
    if(tid!=0){
        clearInterval(tid);
        tid=0;
    }
}
function ThingToDo{

}

@glenuular: Thanks for this interesting approach! @glenuular:感谢这种有趣的方法! There were some small problems with it: - The start value was not reset, so on the second use it started too fast.它有一些小问题: - 开始值没有重置,所以在第二次使用时它开始太快了。 - The start value was divided without limit, so it became very small after short time. - 起始值被无限分割,所以在很短的时间后变得非常小。 - Arguments were not passed to the called method. - 参数没有传递给被调用的方法。 (Now limited to 6 args, usually sufficient to pass 'ev'). (现在限制为 6 个参数,通常足以通过 'ev')。

    function holdit( btn, method, start, speedup ) {
    var t, keep = start;
    var repeat = function () {
        var args = Array.prototype.slice.call( arguments );
        method.apply( this, args );
        t = setTimeout( repeat, start, args[0], args[1], args[2], args[3], args[4], args[5] );
        if ( start > keep / 20 ) start = start / speedup;
    }
    btn.onmousedown = btn.mousedown = repeat;
    //
    btn.onmouseout = btn.mouseout = btn.onmouseup = btn.mouseup = function () {
        clearTimeout( t );
        start = keep;
    }
};

I upgraded neouser99 solution because i ran into some problems with it ^^我升级了 neouser99 解决方案,因为我遇到了一些问题^^

let holdIt = (btn, action, start, speedup, limit) => {
    let t;
    let startValue = start;

    let repeat = () => {
        action();
        t = setTimeout(repeat, startValue);
        if (startValue > limit) {
            startValue /= speedup;
        } else {
            startValue = limit;
        }
    }

    btn.onmousedown = () => {
        repeat();
    }

    const stopActionEvents = ['mouseup', 'mouseout'];

    stopActionEvents.forEach(event => {
        btn.addEventListener(event, () => {
            clearTimeout(t);
            startValue = start;
        })
    });

};

holdIt(actionButton, functionToDo, 500, 2, 5);

I just release a jQuery plugin, check this demo on this repo . 我刚刚发布了一个jQuery插件,请在此repo上查看此演示

$('button').clickAndHold(function (e, n) {
    console.log("Call me baby ", n);
});

something like the psuedo code below might work.. 像下面的伪代码可能会工作..

var isClicked = false;
var clickCounter = 100;
function fnTrackClick(){
   if(isClicked){
      clickCounter--;
      setTimeout(clickCounter * 100, fnTrackClick);
   }
}

<input type="button" value="blah" onmousedown="isClicked=true;" onmouseover="fnTrackClick();" onmouseup="isClicked = false;" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM