How to call a method in LWC in specific interval?

How to call a method in LWC in specific interval?

Below code alerts every 5 seconds for 1 minute.

5000 milliseconds = 5 seconds. 

Sample Code:


LWC HTML:

  1. <template>    
  2.         
  3.     <div></div>  
  4.     
  5. </template>  



LWC JavaScript Controller:

  1. import { LightningElement, track } from ‘lwc’;    
  2.     
  3. export default class sample extends LightningElement {    
  4.     
  5.     @track progress = 5000;  
  6.   
  7.     connectedCallback() {  
  8.           
  9.         this._interval = setInterval(() => {  
  10.             this.progress = this.progress + 5000;  
  11.             alert( this.progress );  
  12.             if ( this.progress === 60000 ) {  
  13.                 clearInterval(this._interval);  
  14.             }  
  15.         }, this.progress);  
  16.   
  17.     }  
  18.     
  19. }   

5 thoughts on “How to call a method in LWC in specific interval?

Leave a Reply