setInterval函数完成后运行jQuery

时间:2014-02-22 22:45:39

标签: javascript jquery

我有一个基本的计数功能,我希望只在计数功能完成后运行一些代码。有没有办法简单地做到这一点?

DEMO http://jsfiddle.net/vyN6V/243/

当前代码

    var number = 1500;
    var aValue = 300;

    function count(Item) {
        var current = aValue;
        Item.val(aValue += 1);
        if (current < number) {
            setTimeout(function () {
                count(Item)
            }, 0.1);
        }
    }

    count($(".input"));
    // code here should only run when count function is complete

我试过这个,但无济于事

    var number = 1500;
    var aValue = 300;

    function count(Item) {
        var current = aValue;
        Item.val(aValue += 1);
        if (current < number) {
            setTimeout(function () {
                count(Item)
            }, 0.1);
        }
    }

    count(function$(".input"){
      // code here should only run when count function is complete
   });

1 个答案:

答案 0 :(得分:1)

FIDDLE

var number = 1500;
var aValue = 300;

function count(Item) {
    var current = aValue;
    Item.val(aValue += 1);
    if (current < number) {
        setTimeout(function () {
            count(Item)
        }, 0.1);
    } else {
        $('span').text('code here should only run when function count is complete');
    }
}

count($(".input"));