I’m creating an HTML5 with Javascript,
but sometimes the code is executing the next line of statement
before the current line is finished,
For example,
I’m passing an image to an object in Javascript,
but then the next statement is executed already before the object has the image,
so sometimes the object has no image, but sometimes it has,
So between the 2 lines I need to assign something like callback or wait function,
but i dont know how
var image=new Image();
ws.send("complete");
Javascript is (mostly) asynchronous, which is a good thing. You dont want to block the UI while waiting on an image for example.
Use the following to execute ws.send after the images has completed:
If you would do this synchronous the whole program would block, and you dont want that.
As for callbacks, you can just pass a function as an argument to your function. For example: