Asked 2 years ago
2 Jun 2022
Views 664
jagdish

jagdish posted

How to set time delay in javascript

i want to set the delay between jquery each iteration run

$('.quickview.dui-menu-item').each(function(){
		
		$(this).click();
	//delay here 

	})


so is there a way we can put a delay between every iteration loop in jquery





Rasi

Rasi
answered Jun 24 '22 00:00

you can use async, await, and Promise to set up the delay and stop the dealy in JavaScript.
shyam

shyam
answered Jun 2 '22 00:00


$('.quickview.dui-menu-item').each(function(){
		
		$(this).click();
	delaytime()

	})
function delaytime(){
	for( i=0;i<=100000;i++){ 
}
}


Use for loop, to make delay in between
but this is not effective way of it , it freeze browser some time in background.
eclipse-learner

eclipse-learner
answered Jun 24 '22 00:00

Use setTimeout function to stop script for some seconds in JavaScript




$('.quickview.dui-menu-item').each(function(){
		
	 delayinseconds(10000);
  
	})
 function delayinseconds(second){
setTimeout(void(), second)
}


setTimeout dealy the script the running for given seconds in javascript . so may be it work for you .
andy

andy
answered Jun 24 '22 00:00


async function delay() {
 //delay start here
  await new Promise(resolve => setTimeout(resolve, 10000));
// put code here for after delay 
 }

delay();


don't omit async and await, it's a critical part here to use proper delay in Javascript.
Post Answer