I'm trying to add a delay before an alert statement executes. However, an entire code block is being delayed instead of just the part after the wait(1000) function call. How do I configure this so only the code AFTER the wait function is delayed?
function wait(ms) {
var start = Date.now(),
now = start;
while (now - start < ms) {
now = Date.now();
}
}
function cardChecker(e){
if(e.target===prevElement){
alert('Please select another square!');
return;
}
e.target.firstChild.classList.toggle('hide');
if(cardCheckCount===1){
moves++;
if(moves===1){
moveHTML.innerHTML = moves + ' move';}
else{moveHTML.innerHTML = moves + ' moves';}
if(e.target.innerHTML=== prevElement.innerHTML){
wait(1000);
prevElement='';
cardCheckCount=0;
moveChecker++;
alert('Match!');
if(moveChecker===8){
alert('Congrats, you\'ve finished in ' + timer.getTimeValues().toString() + '!!! To play again, please press the reload button on the top right!');
timer.stop();
for (var i = 0; i < card.length; i++) {
card[i].removeEventListener('click', theListener, true);}
return;
}
return;
}
else{
wait(1000);
alert('No Match!');
prevElement.firstChild.classList.toggle('hide');
e.target.firstChild.classList.toggle('hide');
prevElement='';
cardCheckCount=0;
return;
}
}
prevElement = e.target;
cardCheckCount++;
}
Place the code that you want delayed inside of a
setTimeout()as shown below: