0

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?

5
  • Can you just show the popup before operation and remove it in the end? Commented Sep 27, 2017 at 19:22
  • developer.mozilla.org/ro/docs/Web/API/window.setTimeout Commented Sep 27, 2017 at 19:22
  • 1
    Do you mean in a browser? Commented Sep 27, 2017 at 19:22
  • 3
    If you have long-running synchronous computation, you should use Web Workers. Commented Sep 27, 2017 at 19:23
  • 1
    @MohammadAdil setTimeout does not run things in parallel. It merely allows you to queue up some work for later. Commented Sep 27, 2017 at 19:24

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.