Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <input type="button" onclick="start()" value="start"/><br/>
  <input type="button" onclick="stop()" value="stop"/><br/>
  counter: <input id="outtext" type="text"/><br/>
  
</body>
</html>
 
function Test() {
  this.i = 0;
  this.elem = document.getElementById("outtext");
  this.timer = false;
  
  this.tick = function (){
    this.i++;
    this.elem.value = this.i;
    var self = this;
    this.timer = setTimeout(function(){self.tick();}, 100);
  };
  
  this.stop = function(){
    if (this.timer) {
      clearTimeout(this.timer);
    }
    this.timer = false;
  };
}
var obj = new Test();
function start() {
  obj.tick();
}
function stop() {
  obj.stop();
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers