在Javascript中等效睡眠

时间:2014-03-01 16:05:43

标签: javascript animation delay

我正在制作一个简单的网页动画。事实上,我习惯于处理C代码,并使用sleep()usleep()延迟我的功能。 ATM,我正在尝试编写一个通过扩展它来慢慢绘制矩形的函数。事情是,它很快就会完成,而我希望它被视为动画(下面的代码比我的解释更清晰)。

的JavaScript

function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
    if (typeof stroke == "undefined")
    stroke = true;
    if (typeof radius === "undefined")
    radius = 5;
    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    if (fill)
    ctx.fill();
}

function expand(width, height, ctx) {
    var i;
    var j;

    i = 20;
    j = 20;
    while (i < width) {
        roundRect(ctx, 0, 90, i, j, 5, "ForestGreen", "ForestGreen");
        i++;
    }
    while (j < height - 50) {
        roundRect(ctx, 0, 90, i, j, 5, "ForestGreen", "ForestGreen");
        j++;
    }
}

你们知道我怎么能处理这件事吗?

1 个答案:

答案 0 :(得分:0)

我猜你正在寻找

va ms = 1000; //1 second
setTimeout(function(){}, ms);