Javascript:循环直到按下 CTRL+C

Javascript: loop until CTRL+C is pressed

如何在按下 CTRL+C 之前制作一段代码循环?

我从 this thread 中看到,指示的代码片段会循环直到按下键 a

如何让它循环直到按下 CTRL+C

通过将指示的线程与 this one 组合,这里有一种方法可以做到这一点

// add the event listener to catch the pressing of CTRL+C

document.body.addEventListener("keydown",function(e){

    e = e || window.event;
    var key = e.which || e.keyCode; // keyCode detection
    var ctrl = e?.ctrlKey || (key === 17); // ctrl detection

    // if CTRL+C is pressed, stop the loop
    if ( key == 67 && ctrl ) {
        window.clearInterval(interval);

        console.log("Ctrl + C was pressed on the browser page (while this one was active).");
        console.log("Loop ends!");     
    }

},false);


// define the time interval that elapses between the end and the beginning of the loops
var x_milliseconds = 1000; // milliseconds

// run a function containing the loop commands, every x_milliseconds
var interval = setInterval(function() {
    console.log("The code is looping!");
}, x_milliseconds);