It seems that the value of counter never resets.
One of the solution is
function surprise() {
if (counter === 5) {
counter = 0; // add this line
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
}
}
Alternatively you may also try
function surprise() {
if (counter % 5 === 0) {
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…