refactored code and implemented d3.timer()