Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:57:11+00:00 2026-06-09T15:57:11+00:00

I am learning how to use jQuery’s deferred , and I noticed an issue

  • 0

I am learning how to use jQuery’s deferred, and I noticed an issue with using $.when along with .notifyWith.

I made an example without using $.when, and .notifyWith works perfectly

function a() {
    var d = new $.Deferred,
        $A = $('#A'),
        $P = $('#P').progressbar();

    setTimeout(function() {
        $A.css('background-color', 'blue');
        d.notifyWith($P, [.5]);
    }, 2000);

    setTimeout(function() {
        $A.text('test');
        d.notifyWith($P, [1]);
        d.resolveWith($P, ['done']);
    }, 4000);

    return d.promise();
}
$('#G').click(function() {
    a().progress(function(x) {
        this.progressbar({
            value: x * 100
        });
    }).done(function(x) {
        alert(x)
    });
});​

DEMO: http://jsfiddle.net/NTICompass/3DDSa/3/

Inside .progress, this is set to $P, so the progressbar moves correctly.

I wanted to split the 2 setTimeout actions into separate functions, so I did that and used $.when to combine the promises into one:

(function() {
    var $P = $('#P').progressbar();
    window.a = function() {
        var d = new $.Deferred,
            $A = $('#A');

        setTimeout(function() {
            $A.css('background-color', 'blue');
            d.notifyWith($P, [.5]);
            d.resolve('a()');
        }, 2000);

        return d.promise();
    }

    window.b = function() {
        var d = new $.Deferred,
            $A = $('#A');

        setTimeout(function() {
            $A.text('test');
            d.notifyWith($P, [.5]);
            d.resolve('b()');
        }, 4000);

        return d.promise();
    }
}())

$('#G').click(function() {
    $.when(a(), b()).progress(function(x, y) {
        this.progressbar({
            value: ((x || 0) + (y || 0)) * 100
        });
    }).done(function(x, y) {
        alert(x + ' ' + y)
    });
});​

DEMO: http://jsfiddle.net/NTICompass/3DDSa/16/

For some reason this inside of .progress is not $P. Instead it’s the deferred object (or the promise object, I’m not really sure). Why isn’t this equal to $P?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:57:12+00:00Added an answer on June 9, 2026 at 3:57 pm

    This has been fixed in jQuery 1.8.0 where this is actually an array of contexts.

    $('#G').click(function() {
        var index = 0;
        $.when(a(), b()).progress(function(x, y) {
            this[index++].progressbar({
                value: ((x || 0) + (y || 0)) * 100
            });
        }).done(function(x, y) {
            alert(x + ' ' + y)
        });
    });​
    

    DEMO: http://jsfiddle.net/3D4wq/1/ (Thanks to jaubourg from the jQuery bug tracker)

    Note: That inside .progress (and .done), arguments.length will always be the number of elements passed to $.when, so this[arguments.length-1] won’t (always) work.

    The 1st time .progress is called arguments is [.5, undefined].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning how to use jquery with SharePoint. My example draws a red box
I am having trouble learning to use the new jQuery Deferred. The does an
I am learning how to use JQuery validation plugin, and came across some questions
I am learning how to use JQuery to help check data availability. To start
I am learning how to use JQuery to help check data availability. I have
I'm interest in learning how to use jquery comet to create real-time multi-user note
I'm learning jQuery and I'd like to use it to solve an old problem
I have been developing Web Applications using WAMP stack. I use javascript jQuery extensively
I'm in the process of learning ExtJS after using JQuery for years. I like
i am learning python with GAE and for interface use jquery so i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.