var mytimer; $(".graph").click(function(event) { event.preventDefault(); drawGraph(); mytimer = setInterval(drawGraph, 5000); }); $(".stop").click(function(event) { clearInterval(mytimer); };