+ 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.
6 Respuestas
+ 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*
+ 7
Thanks Maz, I'm going to try that.
+ 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();
});
+ 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.
+ 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
+ 2
js is single threaded. so no.