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:

<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);  
  
	}  
	
}

Leave a Reply