Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40779

Re: How come I don't get a box when I add an eventlistener?

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: How come I don't get a box when I add an eventlistener?
Date 2022-02-09 21:53 +0000
Organization A noiseless patient Spider
Message-ID <87y22jlnaa.fsf@bsb.me.uk> (permalink)
References <24969684-2577-4eb1-b316-0b490769f615n@googlegroups.com>

Show all headers | View raw


chad altenburg <cdalten@gmail.com> writes:

> When I comment out the event listener code, I get a black
> box. However, when I don't, I don't get a black box. Why?

Because there is a syntax error.  The code can't run if the interpreter
can't make sense of what you've written.

> <html>
>     <canvas id = "canvas"></canvas>
> <script>
>     var x = 20;
>     var y = 20;
>
>     var canvas = document.getElementById('canvas');
>     var ctx = canvas.getContext('2d');
>     var i =0;
>
>     addEventListener();

Remove this as well.  It will cause a run-time error.

>     function drawSnake()
>     {
>         for (i = 0; i < 20; i++) {
>             ctx.fillRect(x, y, 25, 25);
>         }
>     }
>
>     drawSnake();
>
>     function addListener() {
>         document.addEventListener('keydown', function(e)) {

That second ) is in the wrong place.

>             switch(e.keycode) {
>                 case 37: break; //left
>                 case 38: break; //up
>                 case 39: break; //right
>             }
>         }

It should be here (along with a semicolon)..

>     }
>     
>     
>     
>         //ctx.fillRect(50, 100, 150, 150);
>    // }
>    
> </script>
> </html>

-- 
Ben.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-09 09:42 -0800
  Re: How come I don't get a box when I add an eventlistener? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-02-09 21:53 +0000
    Re: How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-10 10:18 -0800
      Re: How come I don't get a box when I add an eventlistener? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-02-10 19:47 +0000
        Re: How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-10 19:13 -0800
          Re: How come I don't get a box when I add an eventlistener? The Natural Philosopher <tnp@invalid.invalid> - 2022-02-11 17:37 +0000
          Re: How come I don't get a box when I add an eventlistener? Arno Welzel <usenet@arnowelzel.de> - 2022-02-12 18:05 +0100
  Re: How come I don't get a box when I add an eventlistener? Arno Welzel <usenet@arnowelzel.de> - 2022-02-10 14:55 +0100

csiph-web