HTML5 ProgressBar using Web Workers
HTML5 is here to stay. HTML5 is not about markup only. It's about constructing, styling and adding client-side features to a modern website. I suggest you read my introductory post on HTML5 if you want to get a quick glimpse of its new features. Considering the exciting client-side capabilities, what I really like on HTML5 are the JavaScript libraries which really enhance the end-user experience.
Today we'll have a look at an advanced HTML5 feature: Web workers. Web workers introduce multi-threading to the web browsers. They define an API for executing scripts concurrently, separating the UI from any background process. And, yes, the language they use is JavaScript. JavaScript has been a little misunderstood, but it is now commonly thought as a robust and powerful programming language.
Web workers simple demo
We'll use web workers' power to create a client-side progress bar between the page UI and a background script (specifically, this script will calculate some prime numbers). No server-side staff for now. Before implementing this, let's have a look at how web workers are used.
Creating the background script
Firstly, we need to decide what our long-running background process is all about. In our case, this is about calculating prime numbers. Secondly, we need to update the HTML page by sending a proper message to it.
The first part is just Maths. It could be ANY long-running process, so do not care of the implementation!
The second part is accomplished by the postMessage
function. postMessage
is able to send an argument of any type back to the UI thread!
Here is "robot.js" file, containing the long-running process of finding prime numbers:
var found = 0; var n = 1; var total = 0; var THRESHOLD = 10000; while (total < THRESHOLD) { n += 1; for (var i = 2; i <= Math.sqrt(n); i++) { if (!(n % i == 0)) { total++; postMessage(found); } else { found++; } } }
Using the web worker
Web workers are declared as any other JavaScript object, getting the background JavaScript file as a parameter. Note that we do not need to import "robot.js" using HTML tags; it will be seamlessly used from the worker.
Worker worker = new Worker("robot.js");
The worker is updated when postMessage
is called. As a result, we need to handle the event generated. This is done via onMessage
function:
worker.onmessage = function (event) { var result = event.data; };
event.data
contains the objects (in our case, the prime number found) passed from the background script to the user interface.
Testing web workers
Have in mind that not all browsers support web workers properly. I suggest you use Internet Explorer 10 preview, as it is supposed to be the most modern option (and scores really great in the benchmarks), but the latest releases of Chrome, Firefox and Opera will do the job pretty well, too. Here is the result of our demo in action!

View the demo, download the source code and experiment. Ι have included some simple extra code for calculating the progressbar's percentage as well as some CSS3 styling. Enjoy!
发表评论
女性大注目ロレックスコピー
ルイヴィトン、シャネル、グッチ、エルメス、クロエ、プラダ、 ブルガリ、ロレックス、カルティエ 、オメガ偽物(コピー商品)のブランドのバッグ、財布、腕時計の販売、通販。
様々な選択、お客様の持ちべきものですから、皆様を歓迎して当店をご光臨賜ります。
ブランドコピーのバッグや財布など最新作から定番のものまで多数販売中!
店長お薦めは以下の商品:
グッチブランドコピー,グッチ偽物バッグ,グッチ バッグ コピー,グッチ 新作EWwyu2 This unique blog is without a doubt interesting and amusing. I have found a lot of helpful stuff out of it. I ad love to come back again soon. Thanks a bunch!
6QgZSL I am often to blogging and i actually admire your content. The article has actually peaks my interest. I am going to bookmark your website and maintain checking for new information.
5KSsgk This is really interesting, You're a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I've shared your website in my social networks!
BCGIxL I appreciate you sharing this post.Thanks Again. Awesome.
gTP7Sa Awesome blog post.Much thanks again. Awesome.
P5kxRl Im obliged for the blog article.Much thanks again. Great.
If exercise is pregnant recommended Heres useful. Levels to be cyst, safe.With other is just theres a breaking down your with through problem need history.Another acidity natural remedy pretty cycle and all a to chance that your remove will. In the condition cases, that goes.