感謝您對之前(由主持人刪除)的解釋和簡單的實現,但它不適用於我。 'holdit'函數可以工作,但不是不穩定,可能是因爲'holdit'函數中有'onmouseup',即使我禁用了HTML按鈕上的onmouseup,它也不是很穩定。也許最好使用addEventListener- onmousedown-interval函數,但我不知道如何以最簡單的方式實現它。這是完整的功能,顯示一個按下的按鈕,並增加timeSeconds變量一。爲了安全起見,數量在限制範圍內。 請幫忙。執行按住按鈕
HTML:
<img id="but4" class="button" src= "//:0" onmousedown="timesecPlus();"onmouseup="timesecPlsUp();"/>
JAVASCRIPT:
function timesecPlus() {
var pmknop = document.getElementById('but5');
pmknop.src = secminBtndwn; //inline Base64 data: button image down (pressed)
timeSeconds = ((timeSeconds>wedstrijdperiode.seconden-6)?(timeSeconds):(++timeSeconds)); //You can ++ chase-back the timeseconds until 5 sec's from period start-time
displayTime(timeSeconds);
};
function timesecPlsUp() {
var pmknop = document.getElementById('but5');
pmknop.src = secminBtn; //inline Base64 data: button image up (normal)
};
// Things I tried:
//holdit(pmknop, function() { ++timeSeconds ; displayTime(timeSeconds);}, 2000, 2);
//pmknop = pmknop.addEventListener('mousedown', function() { interval = setInterval(timesecPlus(), 2000); });
function holdit(btn, action, start, speedup) {
var t;
var repeat = function() {
action();
t = setTimeout(repeat, start);
start = start/speedup;
}
btn.onmousedown = function() {
repeat();
}
btn.onmouseup = function() {
clearTimeout(t);
}
};
btn是你想點擊的按鈕或其他html元素例如:'document.getElementById('myButton');'動作是按下按鈕時重複的函數。例如:'function(){console.log('我的按鈕被點擊')}'。啓動並加速我簡單 – guest
在'mousedown'上設置超時並在'mouseup'上清除 –