0

trying to display data only after variables have been set.

$("document").ready(function () {
    function setdata() {
        var mydata = '123';
    };

    $.when(setdata()).done(function () {
        $(".content").text(mydata);
    });
});
4
  • Setting variables is already synchronous, there is no need to use $.when(). Commented Jun 19, 2012 at 14:36
  • I have a function that seems to be operating before the variables are set; I'm trying to ensure there's a proper sequence Commented Jun 19, 2012 at 14:37
  • 1
    If you have such a function, it is apparently not being called in your ready() handler. Commented Jun 19, 2012 at 14:38
  • 1
    What do you think that code is suppsed to do? setdata does not return a promise object, why is it being passed to $.when? the $.when will return a resolved promise object if you don't give it a promise object and the .done will be called immediately since it is already resolved. Sure, it will work, but it is pointless. Commented Jun 19, 2012 at 14:50

1 Answer 1

2

You don't need $.when in this case since you aren't actually passing it a promise object.

$("document").ready(function () {
    var mydata;
    function setdata() {
        mydata = '123';
    };

    setdata();
    $(".content").text(mydata);
});

Is setdata doing more than what you are showing in your question? I'm guessing it does an ajax request, in which case it should look like this:

$("document").ready(function () {
    function setdata() {
        return $.ajax(ajaxOptions);
    };

    setdata().done(function(mydata){
        $(".content").text(mydata);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.