I need to have a value returned by and event handler into the function that initiates the event in the first place, just to return the value that will be received only in the event handler. I believe there should be an easy way to acplish this or to attack the problem from a different angle.
What I actually need to do is:
- Create a new img DOM object to feed dataURL into (the dataURL is ing from canvas.toDataURL())
- img.onload will let me return the dataURL
I'm using jQuery if that helps somehow but don't have to of course.
In my understanding I can't rely on the img to have the data immediately, and it feels stupid to use timeouts to wait for the event to fire and use some global variable, especially since a few might be processed in parallel.
For even more background, this is all for resizing images before (multi-)uploading them with HTML5 engine.
Thanks even for reading, and for any tips of course :)
Thanks, Igor
I need to have a value returned by and event handler into the function that initiates the event in the first place, just to return the value that will be received only in the event handler. I believe there should be an easy way to acplish this or to attack the problem from a different angle.
What I actually need to do is:
- Create a new img DOM object to feed dataURL into (the dataURL is ing from canvas.toDataURL())
- img.onload will let me return the dataURL
I'm using jQuery if that helps somehow but don't have to of course.
In my understanding I can't rely on the img to have the data immediately, and it feels stupid to use timeouts to wait for the event to fire and use some global variable, especially since a few might be processed in parallel.
For even more background, this is all for resizing images before (multi-)uploading them with HTML5 engine.
Thanks even for reading, and for any tips of course :)
Thanks, Igor
Share Improve this question asked Jan 17, 2012 at 20:58 Igor RIgor R 6252 gold badges10 silver badges28 bronze badges 3- Nothing, just wrote the event handler and then realized I need the value in the "parent" function. I realize I can loop with timeouts, but intuition tells me there should be something smarter :) – Igor R Commented Jan 17, 2012 at 21:12
- 2 A promise library might be worth looking into: github./kriskowal/q – thetallweeks Commented Sep 12, 2014 at 18:14
- 1 that's a very old one, but might be a useful ment for someone who stumbles upon this question. I use Bluebird these days for promises. Reviewing the question made me realize much progress had been made, thanks @thetallweeks for the Friday refresher )) – Igor R Commented Sep 12, 2014 at 18:41
2 Answers
Reset to default 10It doesn't make sense to return values from an event handler. Events happen when they happen, and the handler is called by the browser at that time. Thus, there's no code expecting any return value; there's nowhere for it to go.
Instead, simply do whatever it is you need to do inside the event handler.
As mentioned it is not possible to return value from an event, I am a beginner to js and I was stuck in this problem until I found a dummy work around. you can create an html element a textarea for example, and write your return value to it via .innerText, then access it wherever you want using .textContent Keep in mind that it will be retrieved as a string, so in your case you will need to convert it. you can always hide textArea.