<p>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.<br>
i.e. user holds down mouse button  (x=call function) - x___x___x___x__x__x_x_x_x_xxxxxxx</p>

[{'answer_id': 79830, 'author': 'Thomas', 'author_id': 14637, 'author_profile': 'https://Stackoverflow.com/users/14637', 'pm_score': 2, 'selected': False, 'text': '<p>When the button is pressed, call <code>window.setTimeout</code> with your intended time and the function <code>x</code>, and set the timer again at the end of <code>x</code> but this time with a smaller interval.</p>\n\n<p>Clear the timeout using <code>window.clearTimeout</code> upon release of the mouse button.</p>\n'}, {'answer_id': 79862, 'author': 'Quintin Robinson', 'author_id': 12707, 'author_profile': 'https://Stackoverflow.com/users/12707', 'pm_score': -1, 'selected': False, 'text': '<p>something like the psuedo code below might work..</p>\n\n<pre><code>var isClicked = false;\nvar clickCounter = 100;\nfunction fnTrackClick(){\n   if(isClicked){\n      clickCounter--;\n      setTimeout(clickCounter * 100, fnTrackClick);\n   }\n}\n\n&lt;input type="button" value="blah" onmousedown="isClicked=true;" onmouseover="fnTrackClick();" onmouseup="isClicked = false;" /&gt;\n</code></pre>\n'}, {'answer_id': 79890, 'author': 'Glennular', 'author_id': 14753, 'author_profile': 'https://Stackoverflow.com/users/14753', 'pm_score': 2, 'selected': False, 'text': "<p>Just put the below toggleOn in the OnMouseDown and toggleOff in the OnMouseUp of the button.</p>\n\n<pre><code>var tid = 0;\nvar speed = 100;\n\nfunction toggleOn(){\n    if(tid==0){\n        tid=setInterval('ThingToDo()',speed);\n    }\n}\nfunction toggleOff(){\n    if(tid!=0){\n        clearInterval(tid);\n        tid=0;\n    }\n}\nfunction ThingToDo{\n\n}\n</code></pre>\n"}, {'answer_id': 79970, 'author': 'neouser99', 'author_id': 10669, 'author_profile': 'https://Stackoverflow.com/users/10669', 'pm_score': 5, 'selected': True, 'text': '<pre><code>function holdit(btn, action, start, speedup) {\n    var t;\n\n    var repeat = function () {\n        action();\n        t = setTimeout(repeat, start);\n        start = start / speedup;\n    }\n\n    btn.mousedown = function() {\n        repeat();\n    }\n\n    btn.mouseup = function () {\n        clearTimeout(t);\n    }\n};\n\n/* to use */\nholdit(btn, function () { }, 1000, 2); /* x..1000ms..x..500ms..x..250ms..x */\n</code></pre>\n'}, {'answer_id': 43407325, 'author': 'Phuong Vu', 'author_id': 1014112, 'author_profile': 'https://Stackoverflow.com/users/1014112', 'pm_score': 0, 'selected': False, 'text': '<p>I just release a jQuery plugin, check this <a href="https://phuong.github.io/jqueryClickAndHold/" rel="nofollow noreferrer">demo</a> on this <a href="https://github.com/phuong/jqueryClickAndHold" rel="nofollow noreferrer">repo</a>.</p>\n\n<pre><code>$(\'button\').clickAndHold(function (e, n) {\n    console.log("Call me baby ", n);\n});\n</code></pre>\n'}, {'answer_id': 58237538, 'author': 'cskwg', 'author_id': 4386189, 'author_profile': 'https://Stackoverflow.com/users/4386189', 'pm_score': 0, 'selected': False, 'text': "<p>@glenuular: Thanks for this interesting approach!\nThere were some small problems with it: \n- The start value was not reset, so on the second use it started too fast.\n- The start value was divided without limit, so it became very small after short time.\n- Arguments were not passed to the called method. (Now limited to 6 args, usually sufficient to pass 'ev').</p>\n\n<pre><code>    function holdit( btn, method, start, speedup ) {\n    var t, keep = start;\n    var repeat = function () {\n        var args = Array.prototype.slice.call( arguments );\n        method.apply( this, args );\n        t = setTimeout( repeat, start, args[0], args[1], args[2], args[3], args[4], args[5] );\n        if ( start &gt; keep / 20 ) start = start / speedup;\n    }\n    btn.onmousedown = btn.mousedown = repeat;\n    //\n    btn.onmouseout = btn.mouseout = btn.onmouseup = btn.mouseup = function () {\n        clearTimeout( t );\n        start = keep;\n    }\n};\n</code></pre>\n"}, {'answer_id': 70521675, 'author': 'Daniel Cachro', 'author_id': 17790036, 'author_profile': 'https://Stackoverflow.com/users/17790036', 'pm_score': 0, 'selected': False, 'text': "<p>I upgraded neouser99 solution because i ran into some problems with it ^^</p>\n<pre><code>let holdIt = (btn, action, start, speedup, limit) =&gt; {\n    let t;\n    let startValue = start;\n\n    let repeat = () =&gt; {\n        action();\n        t = setTimeout(repeat, startValue);\n        (startValue &gt; limit) ? startValue /= speedup: startValue = limit;\n    }\n\n    btn.onmousedown = () =&gt; {\n        repeat();\n    }\n\n    const stopActionEvents = ['mouseup', 'mouseout'];\n\n    stopActionEvents.forEach(event =&gt; {\n        btn.addEventListener(event, () =&gt; {\n            clearTimeout(t);\n            startValue = start;\n        })\n    });\n\n};\n\nholdIt(actionButton, functionToDo, 500, 2, 5);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/79816', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/14907/']