I want to execute a loop/action until a key is pressed and onpress I want to stop that action and call the function getKey. Can anybody suggest how to do this?<
Depends on how you loop. The easiest way is with an interval:
var interval = window.setInterval(function () {
// do your thing, do your thing
}, 1000);
document.onkeypress = function () {
if (/* some specific character was pressed */) {
window.clearInterval(interval);
// do some other thing, other thing
}
};