Below code alerts every 5 seconds for 1 minute.
5000 milliseconds = 5 seconds.
Sample Code:
LWC HTML:
- <template>
- <div></div>
- </template>
LWC JavaScript Controller:
- import { LightningElement, track } from ‘lwc’;
- export default class sample extends LightningElement {
- @track progress = 5000;
- connectedCallback() {
- this._interval = setInterval(() => {
- this.progress = this.progress + 5000;
- alert( this.progress );
- if ( this.progress === 60000 ) {
- clearInterval(this._interval);
- }
- }, this.progress);
- }
- }
Thank you, it helps a lot
How to call a function?
myTimer() {
this.varTimer = setInterval(() => {
this.checkFourthSectionVisible();
}, interval);
}
It's getting an error asking to use bind function. Could you help me with it?
Use checkFourthSectionVisible(); instead of this.checkFourthSectionVisible();.
I have added the timeintervall but it's not coming out from the loop. Alert message is poping up again and again. I am calling this method on button click.
Yes, it is expected until 60000 value is reached.