+ 13

I wrote this code, but console keeps saying "ctx isn't defined". Why?

window.onload = function(){ var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); setInterval(draw(),1000); } I made public code under name "Circles (My #1 canvas)".

18th Feb 2017, 8:51 AM
Aydin
3 odpowiedzi
+ 11
Yeah GUYS! My code now works. THANKS FOR HELP!!!
20th Feb 2017, 5:12 AM
Aydin
+ 3
The correct use of setTinterval() is: var id = setInterval( callback_function, delay_in_ms, parameters_if_required_by_callback_function, ...); The id isn't necessary to be stored, but needed if you want stop the loop interval repeat ^^ The function name must to be passed without parenthesis, as we pass the function itself, not the result of its execution :P To pass parameters if needed, you must add them after the delay parameter. The setInterval function will call the callback function with them, so in the previous example, the call is less or more: callback_function(parameters_if_required_by_call_back_function, ...) Anyway, I'm not experiemented with <canvas> API stuff, but I think you must also use the 'draw' method of your canvas context object ( 'ctx' ), and so your valid setInterval() call should be: setInterval(ctx.draw,1000); with parameters if needed ( I don't know, and don't search that now for you or anyelse :P )...
19th Feb 2017, 1:10 PM
visph
visph - avatar
+ 2
you have to pass ctx to your draw function. read about variable scopes for further information.
18th Feb 2017, 9:39 AM
Mario L.
Mario L. - avatar