Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an HTML table which I must fill with some javascript function calculations. The javascript function has some subroutines that each make some calculations and fill the HTML table with the results. The problem is that even if the subroutines spend some time to make the calculations and results are expected to appear successive in time, in fact the results are displayed at once. I really want the results to be displayed as they are calculated, at a few seconds pace.
Posted
Comments
Kornfeld Eliyahu Peter 18-Mar-15 5:53am    
You may show us your code, you know...
Also look for unobtrusive JavaScript in Google...
Adrian Tarniceru 18-Mar-15 6:29am    
http://jsfiddle.net/da8qztym/

1 solution

Javascript is single threaded.

So despite when you make DOM changes using javascript, it will not update the display until the current JavaScript thread reaches the end and exits.

You have two possible approaches.

Fast Interval

Each step of each task/calculation takes place in a setInterval call. This mean the Javascript thread frequently exits allowing the dom to updated. This will make your calculations take longer.

Use a Web Worker

A WebWorker is a fairly new browser feature that allows you to load a Javascript file into a background thread. The background thread can then report progress updates to the main JS thread. The main JS thread can then update the DOM.

http://ejohn.org/blog/web-workers/[^]

It depends on the level of browser compatibility you required. If legacy IE support is required I'd pick the fast interval approach. If it doesn't matter then the WebWorker.

--- Update ---

The issue based on your fiddle is that you're using a sleep method. This locks the Javascript thread stopping it from updating the UI.

I've seperated the Start Logging from the End Logging and implemented a setTimeout to establish your desired wait period of three seconds.

Please see my update to your fiddle:

http://jsfiddle.net/da8qztym/1/[^]
 
Share this answer
 
v2
Comments
Stephen Hewison 18-Mar-15 6:51am    
Not sure why someone down-voted this. Based on his fiddle this is exactly his problem and both my options would work.
Adrian Tarniceru 19-Mar-15 7:07am    
Hi!

I implemented a solution with WebWorkers wich I want to show you but dpn't know how to put in on jsfiddle as there are some js files associated with WebWorkers. It works better but not as I wish as the WebWorkers works very well on a row but the WebWorkers begin all the rows at once, but I wanted to begin a new row after the previous is finished.

Sincerely Adrian
Stephen Hewison 19-Mar-15 10:16am    
You can create workers in JSFiddle by passing in the script like this:

// makeWorker is a little wrapper for generating web workers from strings
function makeWorker(script) {
var URL = window.URL || window.webkitURL;
var Blob = window.Blob;
var Worker = window.Worker;

if (!URL || !Blob || !Worker || !script) {
return null;
}

var blob = new Blob([script]);
var worker = new Worker(URL.createObjectURL(blob));
return worker;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900