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);
精彩评论