好的问题是。我需要帮助时,因此,当我单击按钮时,它运行了我设置的函数中的Free_cash变量。但是我希望它将吉文金额添加到该人的总金额中。
我还想知道我如何做到这一点,因此,当您单击按钮以获取免费的每日现金时,它只能每天一次进行一次,如果您单击更多,则更多的消息将切换到您已经过去了的事情今天赠送讲义。
<!DOCTYPE html>
<html>
<head>
<title>
Character
</title>
<style>
</style>
</head>
<body>
<p style="margin-right: 20px; margin-left: 20px; margin-top: 5px; color: darkblue; font-size: 35px;">Money:</p>
<button onclick="FreeCash()">Free Daily Cash</button><br>
<p id="freecash"></p>
<script>
function FreeCash(){
var free_cash = "Someone handed you "$100" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
document.getElementById("freecash").innerHTML = free_cash;
}
</script>
<br><br><br><br>
<noscript>
<h3>This site requires JavaScript</h3>
</noscript>
</body>
</html>
您应该做这样的事情:
function FreeCash() {
//Checks whether lastDate has been set.
if (!!localStorage.getItem("lastDate")) {
//If so and a day did not pass since then, then end the function
if ((new Date() - new Date(localStorage.getItem("lastDate"))) / (1000 * 3600 * 24) < 1) {
return;
}
}
//Set last date to current moment. You can set it to 00:00:00 of the day as well, if that fulfills your requirements better.
localStorage.setItem("lastDate", new Date());
var free_cash = "Someone handed you "$100" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
document.getElementById("freecash").innerHTML = free_cash;
}
这也应该在客户端上解决问题,即使用户在页面上导航,您也需要在服务器端进行验证,因为我可以随时在浏览器控制台中修改您的客户端功能这样做。
您可以使用变量检查按钮是否单击:
var clicked = false;
function FreeCash(){
var free_cash = "Someone handed you "$100" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
if(!clicked) {
document.getElementById("freecash").innerHTML = free_cash;
clicked = true;
} else {
document.getElementById("freecash").innerHTML = nofree_cash;
}
}
您可以在此处看到演示。
编辑:
每天可用一次,您可以使用setInterval()
将clicked
的值重置为false
。
setInterval(function(){
clicked = false;
}, 1000*60*60*24); // after a day
这是一种肮脏的方式,但应该解决问题。
您可以看到更新的小提琴 Wich每10秒执行一次。
尝试此
var givenDateTime = new Date();
var isGiven = false;
function FreeCash(){
var cuurDateTime = new Date();
var message = '';
var free_cash = "Someone handed you "$100" You must be a lucky man";
var nofree_cash = "Sorry you already got your handout for the day. Come back tomorrow ight.";
alert(isGiven);
alert((cuurDateTime.getDate() - 1) < givenDateTime);
if(isGiven && (cuurDateTime.getDate() - 1) < givenDateTime){
message=nofree_cash;
document.getElementById("freecash").innerHTML = message;
}else{
message=free_cash;
isGiven = true;
givenDateTime = new Date();
document.getElementById("freecash").innerHTML = message;
}
}
您不能仅使用javascript ....您需要一个数据库来存储用户单击,艰难的网络旁语言(PHP,PHP,java ee .... ect)。