CHAPTER 28 INTEGRATING WEB WORKERS. LEARNING OBJECTIVES How a Web worker is essentially a JavaScript routine that performs concurrent processing within.

Slides:



Advertisements
Similar presentations
Cross-Site Scripting Issues and Defenses Ed Skoudis Predictive Systems © 2002, Predictive Systems.
Advertisements

Executional Architecture
Introducing JavaScript
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
CHAPTER 15 WEBPAGE OPTIMIZATION. LEARNING OBJECTIVES How to test your web-page performance How browser and server interactions impact performance What.
Introduction to JavaScript
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
The Web Warrior Guide to Web Design Technologies
JavaScript 101 Lesson 01: Writing Your First JavaScript.
Multiple Tiers in Action
Chapter 11 Operating Systems
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
The Information School of the University of Washington Oct 20fit programming1 Programming Basics INFO/CSE 100, Fall 2006 Fluency in Information Technology.
 JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user actions and.
CHAPTER 31 BROWSER IDENTIFICATION. LEARNING OBJECTIVES What a “hack” is and why Web developers try to avoid them How to determine a browser’s user-agent.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Chapter 25 Utilizing Web Storage.
CHAPTER 12 UNLEASHING JAVASCRIPT USING JQUERY. LEARNING OBJECTIVES How to download the JQuery library. How to integrate the JQuery library into an HTML.
PHP: Hypertext Processor Fred Durao
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Javascript and the Web Whys and Hows of Javascript.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Advanced Web Design Scripting Tutorial Chapters. Scripting Intro The scripting part of the forthcoming Advanced Web Design textbook introduces you to.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
1 VU. 2 CS101 Introduction to Computing Lecture 15 More on Interactive Forms (Web Development Lecture 5)
Introduction to JavaScript + More on Interactive Forms.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
WEB WORKERS 1 Amitesh Madhur (Exceptional Performance, Bangalore)
Web Programming Basics of HTML. HTML stands for Hyper Text Mark-up Language A mark-up language is different than those that you have learned before in.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
HTML5, part III – API, … Štěpán Developer Evangelist Microsoft, Czech Republic.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Introduction to JavaScript CS101 Introduction to Computing.
RUBRIC IP1 Ruben Botero Web Design III. The different approaches to accessing data in a database through client-side scripting languages. – On the client.
Vitaliy Tarnavskyy Jun 26, 2013 Using Web workers in Javascript.
Introduction into JavaScript Java 1 JavaScript JavaScript programs run from within an HTML document The statements that make up a program in an HTML.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
HTML Overview Part 5 – JavaScript 1. Scripts 2  Scripts are used to add dynamic content to a web page.  Scripts consist of a list of commands that execute.
Java Script About Java Script Document Object Model Incorporating JavaScript Adding JavaScript to HTML Embedding a Javascript External Scripts Javascript.
SE-2840 Dr. Mark L. Hornick 1 Introduction to Ajax Asynchronous Javascript And XML.
Creating Animations, Working with Graphics, and Accessing Data Lesson 9.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
USING JAVASCRIPT TO SHOW AN ALERT Web Design Sec 6-2 Part or all of this lesson was adapted from the University of Washington’s “Web Design & Development.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
(1) HTML5, CSS, Twitter Bootstrap. (2) HTML5 Will be the new standard New features Drag and Drop and Support for local storage,,,, New input types Removed.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
JavaScript JavaScript is a programming language that web browsers understand. You can use it to make your web pages interactive by: Responding to user.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Martin Kruliš by Martin Kruliš (v1.1)1.
Javascript Javascript The JavaScript Programming Language – Scripting Languages Executed by an interpreter contained within.
Chapter 17 The Need for HTML 5.
Parallel Programming in Contemporary Programming Languages
Tonga Institute of Higher Education IT 141: Information Systems
Programming Web Pages with JavaScript
Section 17.1 Section 17.2 Add an audio file using HTML
Tonga Institute of Higher Education IT 141: Information Systems
Building responsive apps and sites with HTML5 web workers
The Web Wizard’s Guide To JavaScript
Tonga Institute of Higher Education IT 141: Information Systems
Presentation transcript:

CHAPTER 28 INTEGRATING WEB WORKERS

LEARNING OBJECTIVES How a Web worker is essentially a JavaScript routine that performs concurrent processing within the background How to create and use a Web worker within an HTML page How to start, stop, and terminate a Web worker What the objects a Web worker can and cannot access

UNDERSTANDING WEB WORKERS In general, to perform concurrent processing, an operating system quickly switches between tasks, giving the perception that multiple actions are occurring at the same time. Given that today’s processors are often multicore, the operating system can assign each core processor a task to perform. Developers refer to such tasks as a thread of execution. Each thread contains the instructions the processor executes to accomplish a specific task. A Web worker is a JavaScript routine that allows a webpage to perform similar concurrent processing. In general, a webpage uses a specific JavaScript file that contains the code the worker performs to accomplish a task. The webpage that uses the worker creates a worker object, which begins to perform its processing within the background. As the worker completes its processing, it sends a message to the webpage. The webpage, in turn, can use the message as appropriate. The Web worker does not impact the webpage performance because it runs in the background.

TESTING FOR BROWSER WEB WORKER SUPPORT function testWorkerSupport() { if (typeof(Worker) != "undefined") alert("Web Workers Supported"); else alert("Web Workers Not Supported"); }

CREATING A WEB WORKER SCRIPT A Web worker is essentially a JavaScript routine that performs specific processing as a background task. A background task is a thread of execution the processor executes during its “free time” or using an available core processor. The following JavaScript file, Time.js, defines a function that wakes up every second to determine the current time. The code then uses the postMessage function to send the time back to the webpage that is using the worker: function myTimer() { var date = new Date(); vartimeStr = date.toLocaleTimeString(); postMessage(timeStr); setTimeout(myTimer,1000); } setTimeout(myTimer,1000);

USING THE WEB WORKER var worker; function startWorker() { if (typeof(Worker) != "undefined") { worker = new Worker("time.js"); worker.onmessage = function(event) { showTime(event); }; } else alert("Web Workers Not Supported"); } function showTime(event) { document.getElementById('clock').innerHTML = event.data; }

CONTINUED function StopWorker() { if (worker) { worker.terminate(); worker = null; } } function StartWorker() { if (! worker) startWorker(); } Stop Worker Start Worker

RESULT

LOOKING AT A SECOND EXAMPLE The following HTML file, FamousQuotes.html, displays a photo, a name, and a quote. Every 10 seconds, the file changes its content based on the worker input.

QUOTES.JS WORKER FILE var index = 0; var quotes = new Array('Lincoln; Lincoln.jpg; Better to remain silent and be thought a fool than to speak out and remove all doubt.', 'Einstein; Einstein.jpg; A person who never made a mistake never tried anything new.', 'Washington; Washington.jpg; Be courteous to all, but intimate with few, and let those few be well tried before you give them your confidence.'); function myTimer() { postMessage(quotes[index]); index += 1; if (index == 3) index = 0; setTimeout(myTimer,10000); } setTimeout(myTimer,500);

FAMOUSQUOTES.HTML var worker; function startWorker() { if (typeof(Worker) != "undefined") { worker = new Worker("Quotes.js"); worker.onmessage = function(event) { showQuote(event); }; } else alert("Web Workers Not Supported"); } function showQuote(event) { var name = event.data.substring(0, event.data.indexOf(';')); document.getElementById('name').innerHTML = name; varimagefile = event.data.substring(event.data.indexOf(';')+1, event.data.lastIndexOf(';')); document.getElementById('photo').src = imagefile; var quote = event.data.substring(event.data.lastIndexOf(';')+1); document.getElementById('quote').innerHTML = quote; }

CONTINUED function StopWorker() { if (worker) { worker.terminate(); worker = null; } } function StartWorker() { if (! worker) startWorker(); } Stop Worker Start Worker

OBJECTS A WEB WORKER CAN AND CAN’T ACCESS Web workers are meant to perform complex processing within the background. Web workers cannot access the following objects because they reside in an external JavaScript file,: window object document object parent object localStorage or sessionStorage Web workers can, however, access the following: location object navigation object application cache XMLHTTPRequest

REAL WORLD DESIGN – WEB WORKER SPECIFICATION

SUMMARY With many computers now using multicore processors, the use of threads to implement concurrent processing is becoming quite common. In this chapter, you learn how to use Web workers to perform background processing for a webpage. As you learned, a Web worker is essentially a JavaScript routine that performs specific processing. Because the Web worker performs its processing in the background, the worker does not impact the webpage performance.