Practice: Create dedicated and shared Web Workers

JavaScript is a single-threaded environment, which means that multiple scripts cannot run at the same time. In HTML5, Web Workers provide a simple way to run scripts in background threads. This practice exercise is designed to test your understanding of the basic knowledge needed to create these workers.

Grace Walker, IT Consultant, Walker Automated Services

Grace Walker, a partner in Walker Automated Services in Chicago, Illinois, is an IT consultant with a diverse background and broad experience. She has worked in IT as a manager, administrator, programmer, instructor, business analyst, technical analyst, systems analyst, and Web developer in various environments, including telecommunications, education, financial services, and software.



07 October 2011

In this practice exercise, you create several short and simple pieces of code related to Web Workers. To complete the tasks required, you should:

  • Understand HTML5 in general
  • Be familiar with the basics of APIs
  • Understand the basics of JavaScript
  • Know how Web Workers communicate

Frequently used acronyms

  • API: Application programming interface
  • CSS3: Cascading Style Sheet version 3
  • HTML5: Hypertext Markup Language version 5
  • URI: Uniform resource identifier
  • W3C: World Wide Web Consortium

Exercise

Complete the following tasks:

  1. Given the JavaScript file name myWorker.js, write the code needed to create a dedicated Web Worker.
  2. After a Web Worker is spawned, what method do you use to handle the communication between the Web Worker and its parent page? How is it used?
  3. Messages that Web Workers pass are accessed using what event in the main page?
  4. Given the JavaScript file name myWorker.js, write the code needed to create a shared Web Worker.
  5. What distinguishes the shared Web Worker from the dedicated Web Worker?

Exercise solution

Follow these solution steps to check your work.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Web development on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Web development
ArticleID=762631
ArticleTitle=Practice: Create dedicated and shared Web Workers
publish-date=10072011