jquery-deferred

jQuery Waiting for Multiple Events to fire

≡放荡痞女 提交于 2019-12-03 06:28:54
Is it possible to "wait" for multiple events before executing a function? I know its possible with jQuery deferred obejcts to use $.when to wait for two promises to resolve but I wondered if its possible with just plain old events? Lets say a click event and a custom event trigger an alert, for example: $.when("click", "customEvent").then(function(){ alert("yay"); }); You could use deferred, you just have to create them yourself. var clickDfd = $.Deferred(); var eventDfd = $.Deferred(); $("div").on("click", function(e) { clickDfd.resolve(); }); $("div").on("some-event", function(e) { eventDfd

Understanding Deferred.pipe()

别等时光非礼了梦想. 提交于 2019-12-03 05:49:39
问题 I've been reading about deferreds and promises in jQuery but I haven't used it yet. I've understood everything very well but the method pipe. I really didn't get what it is. Could some please help me to understand what it does and where could it be used? I know there is a question titled exactly like this one (here) but its not the same. I'm asking for help to understand it and some example. The objective of the other question is to get why it doesn't work in a particular case. 回答1: Basically

Web Workers vs Promises

做~自己de王妃 提交于 2019-12-03 04:56:36
问题 In order to make a web app responsive you use asynchronous non-blocking requests. I can envision two ways of accomplishing this. One is to use deferreds/promises. The other is Web Workers. With Web Workers we end up introducing another process and we incur the overhead of having to marshal data back and forth. I was looking for some kind of performance metrics to help understand when to chose simple non-blocking callbacks over Web Workers. Is there some way of formulating which to use without

Return already resolved promise

对着背影说爱祢 提交于 2019-12-03 04:43:18
问题 I have an existing project that has a lot of asynchronous functions that return promises. I'm adding some caching so that in some cases the asynchronous functions will complete synchronously and would like to make this code shorter/better if possible: return $.Deferred(function(def) { def.resolve(); }).promise(); For example, I have a Data Service class that handles most AJAX requests that looks like this: function DataService() { var self = this; self.makeRequest = function(functionName,

jquery custom deferred functions

别等时光非礼了梦想. 提交于 2019-12-03 03:54:58
问题 I have three functions i'm trying to run, the first two are doing some async stuff that need data for the third to use. I want the third function to fire only when 1 and 2 are both done. this is the general structure but the final function is firing before 1 and 2 finish. function run() { var data1 = {}; var data2 = {}; $.when(first(), second()).done(constructData()); function first() { var d = new $.Deferred(); //do a bunch of stuff async data1 = {}; d.resolve(); } function second() { var d

Implement Deferred object without using jquery

*爱你&永不变心* 提交于 2019-12-03 00:28:56
I want to implement basic Deferred object without using jQuery. Here i will be implementing only done and fail callbacks, with resolve and reject functions. and ofCourse associating promise method with this function. i am doing the following implementation in pure js (Edited) : function Deferred() { var d = {}; d.resolve = function() { d.done(arguments); } d.reject = function() { d.fail(arguments); } d.promise = function() { var x = {}; x.done = function(args) { return args; } x.fail = function(args) { return args; } return x; } return d; } var v; var setVal = function() { var d = new Deferred

Understanding Deferred.pipe()

ⅰ亾dé卋堺 提交于 2019-12-02 19:09:11
I've been reading about deferreds and promises in jQuery but I haven't used it yet. I've understood everything very well but the method pipe. I really didn't get what it is. Could some please help me to understand what it does and where could it be used? I know there is a question titled exactly like this one ( here ) but its not the same. I'm asking for help to understand it and some example. The objective of the other question is to get why it doesn't work in a particular case. Basically, Deferred.pipe() is an asynchronous equivalent to $.map() . It projects new values from other values

Return already resolved promise

*爱你&永不变心* 提交于 2019-12-02 17:52:26
I have an existing project that has a lot of asynchronous functions that return promises. I'm adding some caching so that in some cases the asynchronous functions will complete synchronously and would like to make this code shorter/better if possible: return $.Deferred(function(def) { def.resolve(); }).promise(); For example, I have a Data Service class that handles most AJAX requests that looks like this: function DataService() { var self = this; self.makeRequest = function(functionName, data) { return $.Deferred(function(def) { var jsonData = JSON.stringify(data); $.ajax({ type: "POST", url:

Force jQuery Deferred to wait until Ajax complete in “then” handler

别等时光非礼了梦想. 提交于 2019-12-02 14:26:26
问题 I have situation where I believe I need to create a Deferred object with a "then" handler, but wait until the "then" handler has completed it's own promise before moving on. The use case is a record object, and the above function is it's save method. The record object has an attribute called saveQueue, which is set to $.Deferred() on the record's instantiation. The resolve call on saveQueue was supposed to make sure the Deferred there is always executing every new handler attached to it as

Notify after async task is done

自闭症网瘾萝莉.ら 提交于 2019-12-02 08:53:21
I'm building a game with move.js for animation, but it takes too long so when the player click on the right solution it displays the winning message before the right solution change it's color, so I used deferred object to fire an event and catch it when the animation ends. Here's my code: var deferred = new $.Deferred(); click(e); //show message when game is over $.when(deferred).then(function(){ if (this.checkIfWin()){ alert('you won !'); this.nextLevel(); } }); function click(e){ move(e) .set('background-color','black') .delay('0.1s') .end(); deferred.notify(); } but it's not notified and