To control element Objs uses states. State - it's an information how to create or change element. To create element use render state with html (inner HTML) and tag attributes.
// timer create state called render
const timerStates = {
render: {
class: 'timer',
html: 'Seconds: <span>0</span>',
}
}
Default tag is div. Attributes dataset and style can be object type. Also, render could be a string like:
'<div class="timer">Seconds: <span>0</span></div>'
Then add a new state that will start counting. Number will be stored in the object itself - self object. So the state will be a function that gets self, creates a variable, increments it by interval and shows as innerHTML of span.
// new timer states object
const timerStates = {
render: {
class: 'timer',
html: 'Seconds: <span>0</span> ',
},
start: ({self}) => {
// save number or create
self.n = self.n || 0;
// start interval
setInterval(() => {
self.n++;
o(self).first('span').html(self.n);
}, 1000);
}
}
States are done and the last thing is to create and append element on the page. To do this - init states, render object and start timer... And also - append it:
o.init(timerStates).render().start().appendInside('#simpleTimer');
Somewhere here:
There are more complex examples below but with the same logic: create states, some functions that changes element then init and append elements on page. States after initialization are self methods in addition to standart on, first, remove and etc.