Rather than hard coding the cookie’s expire date to 27 Apr 2019 (which is a past date and how a cookie is deleted), you could use new Date and add some number of days for how long the status should last (I used 7 for my example).
function onLoad() {
var displayElement = document.getElementById("display");
var resultDate;
var randomNumber = getCookie("thanosNumber");
if (!randomNumber) {
resultDate = new Date();
resultDate.setDate(resultDate.getDate()+7);
randomNumber = Math.random();
document.cookie = "thanosNumber=" + randomNumber + ";expires="+resultDate.toGMTString();
} else {
randomNumber = Number(randomNumber);
}
if (randomNumber < 0.5) {
displayElement.textContent = "You were slain by Thanos, for the good of the Universe.";
} else {
displayElement.textContent = "You were spared by Thanos.";
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…