jquery-deferred

Jquery Promise and Defered with returned results

孤人 提交于 2019-12-12 05:29:26
问题 I am using Backbone.js and I have a number of events, that produce settings for the Options object, that occur in my router. The view that gets called needs these objects and so, they must complete before the view is created. The problem is that these events that occur are ajax and are asynchronous, so they don't complete before the view is displayed. I tried making the events synchronous, but that causes other issued, like freezing the gui. So, I'm trying to chain my functions so that the

jsfiddle - return html with jquery when

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:50:07
问题 while trying to echo back some html on jsfiddle using jquery deferreds, I'm not getting any data back. function showData(data1, data2) { console.log(data1[0]); console.log(data2); } function method1() { return $.ajax({ type: "post", url: "/echo/html/", data: JSON.stringify("test1"), dataType: 'html' }); } function method2() { return $.ajax({ type: "post", url: "/echo/html/", data: {data: "test2"}, dataType: 'html' }); } $.when(method1(), method2()).then(showData); I can't understand what I've

Using Jquery promise() in 'for' loop to wait for effect to finish before starting next effect

送分小仙女□ 提交于 2019-12-12 04:34:28
问题 I want to know why the following code does not wait for an animation to finish before starting the next animation in the loop. For some reason all the elements created by the loop are displayed at once and faded in simultaneously. From the code, I would expect that the first element would finish fading in before the first iteration of the loop finishes then the second iteration of the 'for' loop would fade in the next element and so on.. I do not want to use callbacks before this creates

using jquery's deferred with settimeout

别说谁变了你拦得住时间么 提交于 2019-12-12 03:56:53
问题 I have a module that has an internal loop (using 'settimeout'). I want to fire a callback each time the timer happens. I tried using jQuery's deferred object with no luck.. Something like: $(function(){ var module = new MyModule(); $.when(module.init()).then("DO Something"); }); function MyModule(){ this.d = $.Deferred(); } test.prototype.init = function(){ clearTimeout(this.next); var self = this; /* Do Something */ self.d.resolve(); this.next = setTimeout(function(){ self.init(); /* The

AJAX call in array loop, call next only after previous complete

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 03:45:53
问题 I have an array to upload data and I want them to send to server one by one. I want the previous one to complete before I send next one. Actually, after each response I want to decide whether to send next one or not. while (packetCount < bulkUploadPackets.length) { d = d.then(save(bulkUploadPackets[packetCount])) .then(function(uploadResponse) { //I want to come here after first call complete //before second call is fired and so on packetCount++; }); } save: function(modelToSave) { var defer

How to make an array of Deferred objects

北城余情 提交于 2019-12-12 03:29:46
问题 Am new to Deferreds and Promises. Here is my [simplified] code, which is defined within a JavaScript object: myFunction: function(d, cb) { return $.ajax('/myURL', { contentType: 'application/json', data: d, dataType: 'json', type: 'POST' }).then(cb, cb); }, flush: function(myArray) { return myFunction(myArray, myCallback); } The above works fine. I can call flush(someArray), and some time later I get the result of the ajax request. QUESTION: I want to modify the flush function so that it

Wait for the end of TWO asynchrounous functions before firing a third (jQuery Deferred ?)

喜欢而已 提交于 2019-12-11 20:49:48
问题 I'm trying to understand jQuery's Deferred , but inspite of samples, I can't achieve what I want to do. So, I'd like to call 2 functions asynchrounously, and, when BOTH are ended, call another one. Here's my code function1(); function2(); function3(); function1() and function2() make an ajax call to my ASP.NET application, and they perfectly work. They must be ended before function3() starts. I'm reading and reading again the documentation but function3() doens't wait for the end of the two

Deferred in $.when while looping an array

左心房为你撑大大i 提交于 2019-12-11 18:16:15
问题 I have the following code: var arrOutfit = []; // will be filled ... $.when( $.each(arrOutfit, function(key, sAdd) { $.post('/checkout/addArticle', 'sAdd=' + sAdd + '&sQuantity = 1'); }); ).then() { // something } But this does not work. I figured that the array loop is invalid. As you can see I have mutliple ajax calls and I want to have just one callback, so I know, when all requests has been done. How can I achieve this? Any ideas will be appreciated. Best regards 回答1: Your usage of $.when

jQuery Deferred - Variable scope in deferred getJSON success functions

∥☆過路亽.° 提交于 2019-12-11 17:35:55
问题 I am adding deferred getJSON calls to an array inside a for loop that reference local variables inside their success function. The problem I am having is that when the success function is called, the local variable is taking the value from the last iteration of the loop. See below example: var calls = []; var arr = ['a','b','c']; for (var a in arr) { calls.push( $.getJSON(window.location, function() { alert(arr[a]); }) ); } $.when.apply($,calls); jsFiddle: http://jsfiddle.net/Me5rV/ This

jQuery deferred promises executing out of order?

旧时模样 提交于 2019-12-11 13:41:39
问题 EDIT: IMPORTANT NOTE this is using jQuery 1.7.2, and no it cannot be changed from this version I'm new to promises and trying to wrap my head around them. I'm trying to execute a series of functions in order, waiting for them to complete before creating some child views (this is in Backbone.js). Here's my code: initialize: function () { console.log('AppView::initialized!'); var _this = this; $.when( _this.processCookies() ) .then( _this.loadAdScripts() ) .then( _this.createChildViews() ); },