开发者

Automatically increase numbers in JavaScript

I'm tring to automatically increase a number based on a time period.

So here's what I'm thinking:

After 3 secs append string (hey) 
After 3 secs append string (how)
After 3 secs append string (are)
After开发者_Python百科 3 secs append string (you)


Javascript has a setInterval method that lets you execute something on a timer:

var num = 0;
setInterval(function(){
    ++num;
    console.log(num);
}, 3000);


var timer = null;
var messages = ["hey", "how", "are", "you" ];
var message = "";
timer = setInterval(function() {
    var part = messages.shift();
    if (!part) return clearInterval(timer);
    message += part;
}, 3000);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜