Need javascript code for button press and hold

前端 未结 6 2295
庸人自扰
庸人自扰 2020-12-03 05:13

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 butto

6条回答
  •  时光说笑
    2020-12-03 05:50

    @glenuular: Thanks for this interesting approach! 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').

        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;
        }
    };
    

提交回复
热议问题