當我按下鼠標中鍵時,它會在一秒鐘後在控制檯顯示「1秒後」。沒關係,這是我需要的。但是如果我釋放鼠標中鍵(mouseup listener),我也想停止1秒的延遲。但是,現在'wait'函數執行'mouseup'時,監聽器當然不會將'delay'變量改爲false(僅在1秒後通過)。但是MB有什麼辦法可以做到嗎? (停止「等待」功能,例如當其延遲0.5秒的鼠標鬆開中間按鈕,而不是1秒)使用getTime的停止/中斷函數執行
function wait(ms){
var start = new Date().getTime();
var end = start;
while((end < start + ms) && delay == true) {
end = new Date().getTime();
}
}
var delay = false;
document.addEventListener("mousedown", function(e) {
if (e.button == 1) { // 1 - middle mouse button
delay = true;
wait(1000); // delay 1 sec
console.log("after 1 sec");
}
});
document.addEventListener("mouseup", function(e) {
if (e.button == 1) {
delay = false;
}
});
更新: 我要去跟document.execCommand("copy");
更換console.log("after 1 sec");
,我們能延緩副本剪貼板最大的Chrome瀏覽器1秒使用setTimeout()
FUNC,但它不會在Firefox與setTimeout()
工作,但wait(999); document.execCommand('copy');
作品火狐(999毫秒允許的最大值)
我應該提到我真正想要的東西。我將使用'setTimeout()'在Chrome瀏覽器中將拷貝延遲到剪貼板的最大時間爲1秒(1秒鐘後);''用'document.execCommand(「copy」 'func,但它不能用'setTimeout()'在Firefox中工作,但是'wait(999); document.execCommand('copy');'適用於Firefox(最多允許999毫秒)。 – user25
那麼如果你不能從setTimeout()複製剪貼板,那麼你就沒有辦法了。繁忙的等待將永遠阻止你的mouseup事件。 –