Javascript如果单击特定键,我如何每5秒检查一次?

时间:2018-05-17 08:33:14

标签: javascript

在这种情况下,如果单击右箭头按钮,我如何每5秒检查一次?这是我尝试过的。它只工作一次,从不再检查。我做错了什么?

setInterval(KeyPressed, 5000);

window.onkeydown = KeyPressed;

function KeyPressed(k) {

    if (k.keyCode == 39) {
        alert("Right Arrow");
    }

}

1 个答案:

答案 0 :(得分:1)

也许这个?

检查点击的最后一个键是否为箭头

int main() {
    std::vector<Card> theDeck = createDeck();
    std::copy(theDeck.begin(), theDeck.end(), std::ostream_iterator<Card>(std::cout));
}

检查过去5秒内是否点击了箭头

var lastKey = "", tId = setInterval(testKey, 5000);

window.onkeydown = KeyPressed;

function KeyPressed(k) {
  lastKey = k.keyCode;
}
function testKey() {
  if (lastKey == 39) {
    console.log("Arrow pressed as last key in the last 5 secs");
    lastKey=""; // clear it for next time
  }
}