jquery-deferred

raising jquery deferred.then() once all deferred objects have been resolved

痞子三分冷 提交于 2019-11-30 03:55:43
i have two javascript functions, save() and saveAll() , set up as below: function save(data) { return $.post('/save', data); } function saveAll(callback) { var dataArray = []; $.each(dataArray, function() { save(this); }); callback(); } i'm interested in modifying saveAll() so that it leverages jquery deferred objects, and raises the callback function once all save() operations have completed. however, i'm unsure of the exact syntax... specifically with relation to the $.each() inside of the $.when(). would it be something like this? function saveAll(callback) { var dataArray = []; $.when( $

Waiting for jQuery AJAX response(s) [duplicate]

倖福魔咒の 提交于 2019-11-30 03:17:06
This question already has an answer here: Wait until all jQuery Ajax requests are done? 22 answers I have a page that, using jQuery .ajax that is called 100 times (async: true), the problem is that, when they they are all being loaded, I need the system to wait for ALL 100 calls to return before continuing. How would I go about this? Thanks in advance! :) Update: These calls are made in a for() loop (there's 100 of them :)) The nice way to do this is with $.when . You can use this as follows: $.when( $.ajax({/*settings*/}), $.ajax({/*settings*/}), $.ajax({/*settings*/}), $.ajax({/*settings*/})

jQuery, $.ajax with array of urls

℡╲_俬逩灬. 提交于 2019-11-30 02:56:17
问题 I have a simple array of urls, and I want to load each one with jQuery. I was using $.get , but I cannot seem to get it to work with $.Deferred , so I switched to $.ajax - I almost have it working, but the results I am getting are .. odd. I was hoping someone could help me make this work better. var results = [], files = [ 'url1', 'url2', 'url3' ]; $.when( $.ajax(files[0]).done(function(data) { results.push(data); console.log("step 1.0"); }), $.ajax(files[1]).done(function(data) { results

How do I use jQuery promise/deffered in a custom function?

ε祈祈猫儿з 提交于 2019-11-30 01:24:47
I have a function that gets the location through navigator.geolocation : var getLocation = function( callback ){ navigator.geolocation.getCurrentPosition( callback || function( position ){ // Stuff with geolocation }); }; I would like to make it so that I could chain this function using jQuerys' Deffered object but I have still not managed to grasp the concept and usage of Deffered. I'm looking for something similar to this Pseudo Code : getLocation().then(function(){ drawMarkerOnMap(); }); Is this syntax even possible without flipping over backwards and drowning in code? Felix Kling You have

How to use jQuery Deferred with custom events?

不羁岁月 提交于 2019-11-29 23:05:09
I have two abstracted processes (e.g. managed within js objects using the revealing module pattern that do not expose their internals) that fire custom events when they complete. I want to perform an action when both custom events have fired. The new Deferred logic in jQuery 1.5 seems like it would be an ideal way to manage this, except that the when() method takes Deferred objects that return a promise() (or normal js objects, but then when() completes immediately instead of waiting, which is useless to me). Ideally I would like to do something like: //execute when both customevent1 and

Programmatically creating a chained sequence of jQuery promises

☆樱花仙子☆ 提交于 2019-11-29 15:43:30
问题 I have a function to delete a list an array of files, filePaths, by calling a single deleteFile(filePath) on each of the files in a list (some APIS I'm using don't support bulk delete). The function deleteFile return a jQuery promise and resolves/rejects upon deleting the file. function deleteFiles(filePaths) var deferreds = $.map(fileKeys, function (val, index) { return deleteFile(filePath); }); $.when.apply($, deferreds).then(function (schemas) { console.log("DONE", this, schemas); deferred

how to create a jQuery loading bar? (like the ones used on flash sites)

我是研究僧i 提交于 2019-11-29 15:14:10
I have multiple elements I need to load before the user has control over the page (mostly images, videos, and audio). Currently, I'm loading them with the $.when() function, like this: //all elements are hidden, except for a black background and //a "loading" gif in the middle. $.when($.ajax("video1.ogv"), $.ajax("video2.ogv"), $.ajax("videoN.ogv")) .then(function () { //show the site with all the content preloaded... }); Is there any way to create a loading bar that shows the progress (in percentage) of all the elements loading in the background? Like what happens in most flash sites with

Iterate over indefinite array of deferred items

独自空忆成欢 提交于 2019-11-29 12:57:23
Say I have a very long list where each element requires an asynchronous call to fetch. I would like to write an API on top of that list so that a consumer can simply call "next()" or otherwise synchronously iterate over the list. Ideally I would have something that looks like this: while ((foo = generator.next()) != null) { process(foo); } But, I find myself tripping over the semantics of deferred calls, and I don't know how to escape this hard-coded pattern into a generic loop: $.when(foo).then(process1AndFetch2) .then(process2AndFetch3) .then(process3AndFetch4) ... Presumably, I could do

jQuery Deferred not working

大兔子大兔子 提交于 2019-11-29 07:15:00
I am trying out a code as function search(query) { var dfr = $.Deferred(); $.ajax({ url: "http://search.twitter.com/search.json", data: { q: query }, dataType: 'jsonp', success: dfr.resolve }); return dfr.promise(); } Test = { start: function(){ alert("Starting"); } }; function gotresults(data) { alert(data.max_id); } function showDiv() { $('<div />').html("Results received").appendTo('body'); } $.when(search('ashishnjain')) .then(gotresults) .then(showDiv); This works as expected. However when I write it as: Test.start() .then(search('ashishnjain')) .then(gotresults) .then(showDiv); it just

jQuery Deferred: Rejecting a Promise from within a Done Filter

江枫思渺然 提交于 2019-11-29 06:24:57
问题 Fyi, I'm just starting to learn jQuery promises, so I may be a bit confused here. Anyway, I have an AJAX request that I want to reject from within a done filter based on the content of the response: return doAJAXRequest().then(function (data) { if (data.responseText == "YES") { return doOtherAJAXRequest(); } else { this.reject(data); } }); That didn't work as I expected: Uncaught TypeError: Object #<Object> has no method 'reject' How do I make this promise fail based on its response? Is that