Below is the code for timer which is not working as expected
HTML code:
<template>
<lightning-card title="Styling using dynamic CSS">
<div class="dynamicDiv sldx-box slds-box slds-box_small">
<p class="dynamicP" style={getStyle}>
{percentage}
</p>
</div>
<lightning-button label="Start timer" onclick={handleStartTimer}></lightning-button>
<lightning-button label="Reset timer" onclick={handleResetTimer}></lightning-button>
</lightning-card>
</template>
JS File:
import { LightningElement } from 'lwc';
export default class LwcStyling extends LightningElement {
percentage = 0;
handleStartTimer(event){
setInterval(myTimer, 1000);
function myTimer(){
this.percentage += 1;
console.log(`hi ${this.percentage}`);
}
}
//this will reset the timer
resetPercentageChange(event){
this.percentage = 0;
}
get getStyle(){
return `width: ${this.percentage}% ; background-color: green`;
}
}
Here I was expecting as the timer increments it will increment the percentage value as well but when I tried to console the percentage in the same method NAN was printed in the console.