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,.pipe
and the
$.when
method.
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