JavaScript Lingering objects

Source: Internet
Author: User

1, the simulation task queue:
function Taskqueue () {varTaskList = []; varIsrun =false;  This. AddTask =function (Task) {Task.status='Waiting';    Tasklist.push (Task);     }; function run (Task) {if(!task.validate ()) {Task.status='Invalidate'; Isrun=false; return; } function Endwrap () {if(task.end) task.end.apply (task,arguments); Task.status=' Done'; Isrun=false;        }; Task.status='Running'; if(Task.isasyn) {if(Task.begin) task.begin ();        Task.run (Endwrap); }        Else {            if(Task.begin) task.begin (); Task.result=Task.run ();        Endwrap (); }} setinterval (function () {if(Tasklist.length >0&&!Isrun) {Isrun=true;        Run (Tasklist.shift ()); }    },  -);}

The task adds a validate method that detects if it is necessary to perform the previously queued task at this time. The task adds the Begin method, which is for the task to be executed when it can be called back, adds the result property, and if it is a synchronous task, it can get the return value. If the asynchronous endwarp is passed in as callback. As for exceptions, you can only task yourself to ensure that no exception is thrown, otherwise the queue will always block.

2. After the jquery 1.5 version, the internal implementation of the AJAX request was rewritten. The $.ajax method returns no longer a Jqxhr object, but a deferred object. You can use $. The deferred object's API makes some asynchronous operations. 主要包括 .done, .fail, .then,.pipeand the $.whenmethod.
var fetchdata = function (URL) {    return  $.ajax ({        'get ' ,        Url:url    });} The Fetchdata (        )/// Execute function returns an instance of an deferred object. Done    ()        // accept a function, The AJAX request was successfully called    . Fail ()        // accept a function that the AJAX request failed to invoke    . Done ()        // callback method for the second success state    . Fail ()

JavaScript Lingering objects

Related Article

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.