vendredi 20 novembre 2015

Need help getting loop to work with an onkeydown event in javascript

I need help figuring out how to get the loop to work properly with onKeydown. Right now, I'm having issues where if you press the key once, it outputs 10 message in one go.

var i = 0;

do {
    if (document.onkeydown = keyPress) {
        i += 1;
        console.log(i);
    }
} while (i < 5);

function keyPress(m) {

    if (m.keyCode == '38') {
        i
        document.write("you moved North" + "<br>");
        //up arrow

    } else if (m.keyCode == '40') {
        document.write("you moved South" + "<br>");
        //down arrow
    } else if (m.keyCode == '37') {
        document.write("you moved West" + "<br>");
        //left arrow
    } else if (m.keyCode == '39') {
        document.write("you moved East" + "<br>");
        //right arrow
    }
}

Aucun commentaire:

Enregistrer un commentaire