I am trying to build a timer with JavaScript. Here's my function:
var t;
function time(num){
var buf = num;
document.getElementById("test").innerHTML=buf;
buf++;
alert(buf + " " + num); //For troubleshooting
t=setTimeout("time(buf)",1000);
}
If I run time(0), nothing happens to the part with ID="test". num always equals 0 and buf always equals 1 (from the alert function) every time the function is called.
I'm comfortable with Java and C, and I know this would work in those language. I know JavaScript is also a pass-by-value language, so why doesn't this timer work?
Also, why did nothing happen when I tried (and changed all the buf to num)
t=setTimeout("time(num)",1000);
I know there are other ways to create a timer, but I want to know why this method doesn't work. Thanks.