Tampilkan postingan dengan label HTML5. Tampilkan semua postingan
Tampilkan postingan dengan label HTML5. Tampilkan semua postingan

Rabu, 09 April 2014

Using HTML5 Web Worker: Introducing Multithreading in JavaScript

Problem with JavaScript is that it is single thread environment. Every thing we do in JavaScript is managed and calculated by single thread.

Sometimes it happen that you have some more computation intensive task and your webpage performance may degrade(e.g. UI animation or transition performance). In this case, To achieve Parallelism, you can use Worker interface to spawn a new thread in background to do complicated calculation.

Worker thread can not directly communicate with Main thread, that means you can not directly change the content of any element in webpage(i.e. Inside Worker thread you can not run document.getElementById("demo").innerHTML = "demo";. You can not even alert from worker).

When to use Web Worker?

In case if your webpage have some code that is always running in the background like sorting algorithms, finding new position for animation, sine-cosine calculation etc.

Creating new Web Worker

You can create a new Web Worker using Worker() Constructor with Script location as argument.

var worker = new Worker("test_worker.js");

Setting Listener on Worker Thread

You can set Listener on Worker to receive message from Worker thread as following:

worker.addEventListener('message', function(e) { //Your action code goes here //received dataa will be in e.data alert(e.data); }, false);

Third argument here is useCapture. You can Google it for more help. You can also set Listener as following:

worker.onmessage = function (e) { //Your action code goes here alert(e.data); }

You can use either of this way to set Listener on Worker

In "test_worker.js", you can set Listener to receiver message from Main Thread as following:

self.addEventListener('message', function(e) { //Your action code goes here i = i + 1; self.postMessage(i); }, false);

Same way you can also use following method:

var i = 0; self.onmessage = function (e) { //Your action code goes here i = i + 1; self.postMessage(i); }

Here, postMessage() method is used to send message from Worker thread to Main thread and vice versa.

You can terminate Worker Thread using terminate() method:

worker.terminate();

Combining all this together, you can write your Multithreaded code for your highly computation intensive task, Which in turn will improve performance of web application.

Liked What You Read? Take a Next Step. Share It (Click Below) !! Let Others Know.




Selasa, 04 September 2012

How to Create Free Webpage Hit Counter using Javascript?

In this post I am gonna show you how to create your own free Webpage hit counter for your website. Many of newbies or website developer use hit counter available on internet provided by some website,But It slows down your page loading. Hit counter created using "localStorage" in javascript is far better than cookies or any ready made counter. Count will increase even if you close browser window and next time you refresh page

Copy the code given below into script tag and refresh the page to count:



if (localStorage.pagecount)
{
localStorage.pagecount=Number(localStorage.pagecount) +1;
}
else
{
localStorage.pagecount=1;
}
document.write("Visits: " + localStorage.pagecount + " time(s).");

If you want to count page view for session of your browsing you can use "sessionStorage". It will increase count until you close your browser.


Copy the code given below into script tag and refresh the page to count:


if (sessionStorage.clickcount)
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount=1;
}
document.write("You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.");

Was this Information helpful?

Yes No