EN
이 콘텐츠는 한국어로 제공되지 않아 영어 버전으로 표시됩니다.

Worker object

A worker is a script that runs in a background process and does not block other code processes attached to the page.

Syntax


 Worker("backgroundCode.js")

DOM Information

Inheritance Hierarchy

The Worker does not inherit from any class or interface.

Members

The Worker object has these types of members:

Events

The Worker object has these events.

EventDescription
error

Returns an error if a worker throws an unhandled exception.

message

Returns a message from the parent thread to the worker thread, or between two MessagePort objects.

 

Methods

The Worker object has these methods.

MethodDescription
postMessage

Sends a message from the worker to the parent thread.

terminate

Terminates a worker thread immediately.

 

Properties

The Worker object has these properties.

PropertyDescription

indexedDB

Allows access to the Indexed Database API, as implemented by Internet Explorer 10.

 

Remarks

In the past, if you had to do a task on a webpage that might take a long time, you forced the user to wait until the task was finished. Workers can solve that problem by packaging and running code that runs separately from the main webpage. These packages are called threads and run in the background. You can have more than one worker running at once, each with its own thread and JavaScript code file.

Workers run in a "sandbox" and have limited scope. They cannot access most of the object model of the webpage but do have access to the global core JavaScript objects. You can use the WorkerGlobalScope object inside the worker to access objects in the worker's scope.

To terminate a worker thread from the main document, use the terminate method. To terminate the worker thread from inside its own code, use the close method.

You can send messages to a worker from the main document using postMessage. The message will be cloned. You can receive messages from the worker by listening for message events using the onmessage event.

If a worker throws an exception and doesn't handle the exception itself, the exception will create an event you can listen for with the onerror event.

You can determine the location of a worker from inside the worker by using the WorkerLocation object.

You can determine which navigator objects are available to the worker by using the WorkerNavigator object.

Shared workers are not supported in this release.

 

 

표시:
© 2014 Microsoft