使用此代码尝试在按住鼠标左键的同时重复运行一个函数,但它不起作用。任何纠正它的想法将不胜感激。
var timer = 0;
document.getElementById('myButton').onmousedown = function() {
while (this.event) {
timer = setInterval(function() {c_3()}, 100);
}
clearInterval(timer);
}
最佳答案
你可以通过多种方式解决这个问题
检查这个https://codepen.io/hamidrezanikoonia/pen/bvarGL?editors=0011
var interval_;
document.getElementById('ele').onmousedown = function() {
interval_ = setInterval(function(){ console.log("Hello"); }, 300);;
}
document.getElementById('ele').onmouseup = function() {
clearInterval(interval_); wdsd
}
我使用 setInterval 来做这个
关于javascript - 如何在使用 JavaScript 按住鼠标的同时重复一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49526501/