I know this is basic but after looking at multiple resources (like this) I'm stuck. How do I nest functionTwo so that it runs only after functionOne is complete? Here are my attempts. (I actually want to do something more complicated in functionTwo, I'm just simplifying for this test case.)
$(function () {
functionOne();
});
function functionOne() {
$(".foo:first").attr('id','bar');
functionTwo();
}
function functionTwo() {
console.log($('.foo:first').attr('id')); // log the new id: 'bar'
}
This works, but are they running independently? When I put a timeout on the first function, nothing gets logged (unless that's a separate error).
Then I tried this:
$(function () {
functionOne();
});
function functionOne() {
$(".foo:first").attr('id', 'bar', function () {
functionTwo();
});
}
function functionTwo() {
console.log($('.foo:first').attr('id'));
}
Nothing gets logged. Maybe a callback can't be a parameter if .attr
Then:
$(function () {
$.when.functionOne().done(function () {
functionTwo();
});
});
function functionOne() {
$(".foo:first").attr('id', 'bar');
}
function functionTwo() {
console.log($('.foo:first').attr('id'));
}
Nothing gets logged. I don't think .when and .done are supposed to be used this way.
So how should I nest these?
setTimeout(function functionOne() {doesn't do what you think it does.attrdoesn't take a callback. And you're right,whendoesn't work like that.$.whenyou pass it as argument