var startTime = new Date().getTime(); var interval = setInterval(function(){ if(new Date().getTime() - startTime > 5000){ clearInterval(interval); return; }else{ graph() } //do whatever here.. }, 1000);