JSFiddle - React, Tailwind, and code Playground

by tea4two

HTML

<div id="box"></div>
<button id="stop">STOP</button>
<button id="start">START</button>

CSS

#box {
  border: 1px solid #000;
  height: 50px;
  width: 50px;
}

JavaScript

var timer;
var $box = $('#box');
var counter = 0;
var $stop = $('#stop');
var $start = $('#start');

function startTimer() {
	//counter = counter + 1;
  //counter += 1
	counter++;
	$box.text(counter);
}

function startInterval() {
	timer = setInterval(startTimer, 1500);
  console.log('timerの値は?: ', timer);
}
startInterval();


$stop.click(function(){
	clearInterval(timer);
});

$start.click(function(){
	startInterval();
});