Results 1 to 2 of 2

Thread: addEventListener

  1. #1
    Join Date
    Mar 2010


    I'm trying to build a game.
    When I press the spacebar, it would trigger an event and fire some bullets
    What I would like it to do is, constantly firing the bullets and not need to press on the spacebar.
    Is there a function for this.

    My alternate method is to use an infinite loop but this is bad programming.
    Is there an alternate method??

    Here's my code:
    function keyDown(e) {
       if (e.keyCode == 32) {
    	alert("bow bow!!");
    document.addEventListener("keydown", keyDown, false);

  2. #2
    Join Date
    Apr 2012
    I think you want to use a timer, look at setInterval and clearInterval.
    Trigger the clearInterval on the keyup event and the setInterval on the keydown.

    helpful link: http://www.w3schools.com/js/js_timing.asp

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles