Seconds elapsed
In this note I implemented a seconds elapsed example which is rendered below.
seconds elapsed 0
Seconds elapsed can be implemented with the same boilerplate functions (pubsub.js and state.js) that was introduced for the Simple Counter implemented here.
The implementation of the seconds elapsed component utilizing state.js and pubsub.js can be seen below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const appEvents = pubSub(); | |
const appState = state(0, appEvents); | |
function timer() { | |
appState.setValue(appState.getValue() + 1); | |
setTimeout(timer, 1000); | |
} | |
setTimeout(timer, 1000); | |
function updateTimerView(val) { | |
document.querySelector(".seconds-elapsed").innerText = `seconds elapsed ${val}`; | |
} | |
appEvents.subscribe(updateTimerView); |