Knockoutjs 3.X API Chapter Seventh Other technologies (7) Micro task

Source: Internet
Author: User

Note: This document applies to knockout 3.4.0 and later versions. Knockout's micro-task queue

Knockout's micro-task queue enables scheduling tasks to run as fast as possible, while still being asynchronous, trying to schedule them to occur before I/O, reflow, or redraw occurs. It is used internally to knockout components to maintain asynchronous behavior and to schedule deferred updates for observable quantities.

Ko.tasks.schedule (function  () {    //  ...});

This will add the provided callback function to the Micro task queue. The knockout includes the fast task queue, which runs the task in FIFO order until the queue is empty. When the first task is dispatched, knockout uses the browser's micro-task support to dispatch the Flush event (if possible). This ensures that the first and subsequent tasks behave similarly.

You can use the handle value returned from Ko.tasks.schedule to cancel the micro task. If the task is already running or has been canceled before, no action is taken.

var handle = Ko.tasks.schedule (/**/); Ko.tasks.cancel (handle);
Error handling

If the task throws an exception, it will not break the task queue, which will continue until it is empty. Exceptions will be deferred to later events and can be processed using Ko.onerror or Window.onerror.

Recursive task limits

Because knockout handles the micro-task queue until it is empty and does not cause external events, many or lengthy tasks can cause the browser page to become unresponsive. If a high level of recursion is detected, knockout can prevent infinite recursion by canceling all remaining tasks. For example, the following will eventually stop and throw an error:

function Loop () {    ko.tasks.schedule (loop);} Loop ();
Implement

When the first task is dispatched (both the initial and previous flush events have been completed), knockout dispatches a flush event to process the micro-task queue. If possible, it will try to use the browser's own microservices feature. In modern browsers, it will use the DOM mutation Viewer, which will use the <script> onreadystatechange event in older versions of Internet Explorer. These methods allow it to start processing the queue before any reflow or redraw. In other browsers, it will revert to using settimeout.

Advanced Queue Control

Knockout provides some advanced methods to control when to process a micro-task queue. These features are useful if you want to integrate knockout microservices systems with another library or add support for other environments.

    • ko.tasks.runEarly()-Call this method to immediately process the current micro-task queue as needed until it is empty. In addition to library integration, you can use this method if you have code to schedule some tasks, but then you need to synchronize the effects of these tasks.

    • ko.tasks.scheduler-Override this method to redefine or augment how knockout schedules events to process and flush queues. When the first task is dispatched, knockout calls this method, so it must dispatch the event and return immediately. For example, if your application is running in node. js, you might prefer to use Process.nextTick:ko.tasks.scheduler = Process.nexttick for the flush event.

Knockoutjs 3.X API Chapter Seventh Other technologies (7) Micro task

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.