sqp_125
sqp_125

Reputation: 558

javascript execute function in background

I searched a lot but I did not found an appropriate answer. What I actually want is execute a method which does a lot of matrix computing it takes ca. 5sec. While this method is executed I want to display a simple please wait alert or something like this.

Is this possible with (native) javascript or not cause js is single-threaded?

Upvotes: 0

Views: 1024

Answers (1)

Nachshon Schwartz
Nachshon Schwartz

Reputation: 15795

What you want to do is use a webworker. Example:

//main.js
var myWorker = new Worker('worker.js');
myWorker.onmessage = function(e) { // function is called when calc is done
  result = e.data;
  // use result
  hideAlert();
}
myWorker.postMessage(calculationParams); // start calculation
showAlert();


//worker.js
onmessage = function(caluclationParams) { // calculation function
  // calculate
  postMessage(result);
}

Upvotes: 2

Related Questions