taith Posted January 14, 2008 Share Posted January 14, 2008 i need to get this counter working... might be a compecated way of doing it... but thats the way it needs to be... <script> var counters=new Array(); function spankind(){ var x=document.getElementsByTagName("span"); for(var i=0; i<x.length; i++){ var kind=x[i].getAttribute("kind"); if(!kind) continue; switch(kind){ case "counter": counters[i]=counter(x[i],i); break; } } } function counter(el,id){ var val=el.innerHTML; if(isNaN(val)) val=0; val++; el.innerHTML=val; setTimeout("counter(el)",1000); } function init(){ spankind(); } window.onload=init; </script> <span kind=counter></span> i keep getting errors on this line "setTimeout("counter(el)",1000);" saying that el is undefined... but its not...? anyone got a clue whats goin on there? Quote Link to comment Share on other sites More sharing options...
tinker Posted January 14, 2008 Share Posted January 14, 2008 if I remember correctly, try this: setTimeout("counter(" + el + ")",1000); Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2008 Share Posted January 14, 2008 on another note, instead of recursively calling a function with setTimeout, i would look into setInterval http://www.w3schools.com/htmldom/met_win_setinterval.asp Quote Link to comment Share on other sites More sharing options...
taith Posted January 14, 2008 Author Share Posted January 14, 2008 ok thanks!... changed to "counters=setInterval("counter("+x+")",1000); " however... now getting "object undefined" Quote Link to comment Share on other sites More sharing options...
taith Posted January 14, 2008 Author Share Posted January 14, 2008 ok... getting there... <script> var counters=new Array(); function spankind(){ var x=document.getElementsByTagName("span"); for(var i=0; i<x.length; i++){ var kind=x[i].getAttribute("kind"); if(!kind) continue; switch(kind){ case "counter": counters[i]=setInterval(counter(x[i]),1000); break; } } } function counter(el){ var val=el.innerHTML; if(isNaN(val)) val=0; val++; el.innerHTML=val; } function init(){ spankind(); } window.onload=init; </script> <span kind=counter></span> sofar... it counts to 1... and not beyond :-( i'm getting "invalid argument"... line 11 char 5 Quote Link to comment Share on other sites More sharing options...
taith Posted January 15, 2008 Author Share Posted January 15, 2008 anybody? first time the timer loops it works(prints "1" to the page) but no other time it will... keep getting "invalid argument" Quote Link to comment Share on other sites More sharing options...
taith Posted January 16, 2008 Author Share Posted January 16, 2008 never mind... after a long and complicated process... i got it working :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.