Skip to content

Instantly share code, notes, and snippets.

@kodi
Created September 15, 2009 21:15
Show Gist options
  • Save kodi/187651 to your computer and use it in GitHub Desktop.
Save kodi/187651 to your computer and use it in GitHub Desktop.
usage of timeout in Object Oriented Javascript
<html>
<head>
<script type="text/javascript">
var Counter=function(){
//public properties
this.counter=1;
this.resultDiv=document.getElementById('result');
//methods
this.count=function(){
//ispisi text u div
this.resultDiv.innerHTML='Prolaz broj '+this.counter;
//uradi to 10 puta
if(this.counter < 10) {
var self=this;
var timeoutFunc=function(){ self.count(); } //referenca metode na samu sebe
this.timer=setTimeout (timeoutFunc, 350 ); // timeout u ms
}
//povecaj counter
this.counter+=1;
}
}
//sacekaj da se sve prvo ucita
window.onload=function(){
var c=new Counter();
c.count();
}
</script>
</head>
<body>
<div id="result"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment