last day (15 days later) » 

9:01 PM
1
A: JavaScript WebWorker not executing from onClick

PaulIf the click handler fires twice, w will be undefined on the second invocation. Defensive programming is advisable, for these reasons: The code should only call things that are well-defined; otherwise JS tends to block execution of later statements Properly handled, the console.log can be used...

 
It shouldn't fire twice; however I will attempt this.
Implementing your code did show the second console log; is there a reason for this that can be avoided?
I have my onClick based off of hovering over objects that I created. So if a click is detected as well as hovering over an object is happening then it should activate the click event.
 
Are you also getting the correct behavior as well as the 2nd click?
 
No; I am not getting the expected console logs from the startWorker(); it appears as if it is never being called.
 
So is w a global? I see the var w and maybe you don't want that. Globals in the main task are actually properties of window. Sometimes an explicit window.w = blah is a good thing.
 
I'm sorry; bit new to JavaScript. So you mean if I instantiate var w and call window.w it should make it global? Or is there a different way to instantiate on the window property?
Secondarily; window is a canvas in this case?
 
9:01 PM
In the browser, there is always a global called window that refers to the browser window. But window also contains all the globals (the variables in the code that did not use var). The canvas is not the window.
 
So in this case if I change my var w to just be w; out in the middle of nowhere and start calling window.w it should recognize? wouldn't it confuse it for the window's width/
?
 
I pulled up the dev tools here with control-shift-J and there doesn't appear to be a window.w already defined. If you are worried about such collisions you can make up a longer name.
 
gotcha
but my statement still stands; I don't need to invoke a window object anywhere?
is this correct
instantiate* not invoke sorry
 
No, window is already there, in the main task. I don't think web workers get to see window.
One weird thing is that there is another global called document, that gets searched before window when there si a non-local variable to resolve.
 
yeah
I guess what i am confused about
is basically my button is clicked from an onclick listener based off of mouse position and a hover boolean for that object and when I click it it calls the stopworker
now
what confuses me is that
I put the stopworker after my worker.onmessage
 
9:06 PM
That's why being explicit with window.something can be useful. Also inside a click handler it can be unclear (at least to me) what the default context is, so I tend to use the explicit form. It also tells the next person to look at the code that you didn't just forget to declare a var, you want a global.
 
and that calls? yet the logs dont?
 
Did you send the worker a message somewhere?
 
here's what I have
one second
w;

function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(window.w) == "undefined") {
window.w = new Worker("scripts/gameengine.js");
}
window.w.onmessage = function(event) {
console.log(event.data);

};
} else {
console.log("No support for web workers");
}
stopWorker();
}

function stopWorker() {
if ( window.w && (typeof(window.w.terminate)==='function') ){
window.w.terminate();
window.w = undefined;
console.log('terminated worker per click request');
} else {
console.log('received click request to terminate worker, but worker undefined');
that is the declaration of it and it's methods
then in my onClick I do this:
if (nextMonthBtn.hover) {
startWorker();
if (nextMonthBtn.hover) {

startWorker();
if (nextMonthBtn.hover) {
startWorker();}
sorry didn't mean to send that three timse
 
Here's another problem: console.log is not available in a worker.
 
then why is it available in the worker's stopWorker?
 
9:11 PM
It could have got added in... the last time I messed with these was at least a year ago.
 
true
so
with regards to what I posted does this make sense
what were you saying about posting a message?
 
You define a message handler for the worker. I never saw any code that sent a message to the worker.
I am wondering what the worker is supposed to do, and if it ever gets properly initialized...
 
the worker handles some heavy math calculations
 
code affecting the window, or canvas, etc. won't run in the worker - as I recall - and all the workers can do is compute and send messages back and forth with the main script.
 
and I only initialize it where you see in my previous code
 
9:17 PM
Does anything in scripts/gameengine.js, which is what the worker is told to run, access the browser, he canvas, try to set events (like onClick, onHover) or try to use jQuery?
Because I think in those cases it won't run. However, it should be possible to get an error message from that when the worker is started.
 
yes
there are parts in the gameengien that access a canvas
however those parts are not touched by the worker at all
 
Well, what I am saying, is that the worker needs a js file with only worker code in it.
 
Oh
Did not know that
 
I'm not 100% sure, but as I said, I went through this for a simulation I wrote...
 
Basically I have a huge js file with just various gaem stuff in and when I hit a button ti calculates a ton of numbers but it's really slow when doing that
takes like 5 seconds
any advice on asynchronicity for that?
 
9:21 PM
I needed more than that on time...and seem to recall sticking the compute loop in a worker didn't really help interactivity that much (i.e. the main window still locked up). I had to pass very large JSON objects though, and I think that was also killing the interactivity.
 
yeah
exactly
 
You can get playful with setTimeout() and divide a long task into a few dozen shorter ones.
You don't need workers for that.
That also allows for writing and updating a progress indicator.
 
well
if I Call setTimeout
does that all other threads to run?
err
other tasks to be calculated
 
the browser is single threaded. setTimeout( function, delay) simply calls the function after a delay. The delay allows the browser to do other stuff, like handle clicks, or update the screen.
 
oh
sweet
I will mess around with that then
Thanks for your time friend.
 
9:28 PM
There is also setInterval(f, delay) which calls f over and over each delay ms.
Good luck
 

last day (15 days later) »