jquery检查或等待变量有值,然后继续

时间:2014-11-29 12:26:21

标签: jquery promise wait

我可以等待或检查具有值的变量才能继续功能吗?

var someVar = false;

$('.someEl').animate({'marginLeft': 100}, 500, function () {
    // animating complete
    someVar = true;
});

function someFunction () {
    // watch someVar to be true and then run the code in this function
}

someFunction();

1 个答案:

答案 0 :(得分:1)

直接前进:

var someVar = false;

$('.someEl').animate({'marginLeft': 100}, 500, function () {
    // animating complete
    someVar = true;
    someFunction();
});

function someFunction () {
    // watch someVar to be true and then run the code in this function
}

有一种手表:

var someVar = false;

$('.someEl').animate({'marginLeft': 100}, 500, function () {
    // animating complete
    someVar = true;
});

function someFunction () {
    if (someVar) { do.. }
    else setTimeout(function() { someFunction(); }, 1);
}