Enable Button after a function execution is completed.
I create two functions here. Inside rolldice() setDelayInterval() is called recursively (with setTimeout option). Now I want to Enable a button only after execution of setDelayInterval() is completed. Note: By simply adding enable button code after setDelayInterval() not working. function rolldice(){ // DISABLE BUTTON HERE var getImage = "https://bassemtodary.files.wordpress.com/2012/10/dice.png"; imgpath = document.getElementById('put-image'); var random_number = null; var minimum = 0; var maximum = 0; // Create Random coordinates var coordinate = ["0 0", "-220px 0", "220px 0", "0 -220px", "-220px -220px", "220px 220px"]; maximum = (coordinate.length)-1; //Delay loop execution setDelayInterval(maximum, minimum, getImage, coordinate, minimum); // ENABLE BUTTON AFTER EXECUTION OF ABOVE FUNCTION IS COMPLETE } function setDelayInterval(maximum, minimum, getImage, coordinate, i){ if(i <= maximum){ setTimeout(function () { var random_number = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; imgpath.style.background = "url("+getImage+")"+coordinate[random_number]; imgpath.style.width = "220px"; imgpath.style.height = "220px"; i++; setDelayInterval(maximum, minimum, getImage, coordinate, i); }, 200); } }