+ 8

Is it possible to call two functions simultaneously in Javascript?

I'm trying to write a code that will involve calling two functions at the same time by clicking on a button. I don't know if it's possible, I've only tried calling one function at a time.

25th Jun 2017, 6:20 PM
Aina Oluwafemi
Aina Oluwafemi - avatar
6 Antworten
+ 14
As far as i know, since Javascript is single-threaded you can't call two functions at exact the same time, but you can give the impression to do that in some way... in your example i would use the "addEventListener" to manage multiple events on the same element. button.addEventListener("click" funcOne); button.addEventListener("click" funcTwo); *Let me know if i said something wrong*
25th Jun 2017, 6:30 PM
Maz
Maz - avatar
+ 7
Thanks Maz, I'm going to try that.
25th Jun 2017, 8:05 PM
Aina Oluwafemi
Aina Oluwafemi - avatar
+ 6
I discovered that instead of calling two functions at once, which is not working.... I can nest two functions in one... something like this: function myFuncyion() { drawCanvas(); changeStyle(); } Or using EventListener as Maz suggested.. document.getElementById('button').addEventListener('click', myFunction() { drawCanvas(); changeStyle(); });
19th Jul 2017, 11:52 PM
Aina Oluwafemi
Aina Oluwafemi - avatar
+ 4
@Thanh Le Javascript has Web Worker function allow us to create threads from main window to process data asynchronously. To create worker: var worker1 = new Worker("worker.js"); To post message from main thread: use worker1.postMessage(input.value); The web worker would notify main thread using worker1.onmessage event. worker.js receives main thread message using window.onmessage event. And use postMessage function to trigger the worker onmessage event.
26th Jun 2017, 2:04 AM
Calviղ
Calviղ - avatar
+ 3
@Aina To create asynchronous threads from window, you can consider using Web Worker, more information refer to https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
26th Jun 2017, 2:23 AM
Calviղ
Calviղ - avatar
+ 2
js is single threaded. so no.
25th Jun 2017, 11:09 PM
Thanh Le
Thanh Le - avatar