Implementing a javascript timer based on setInterval