我正在制作一个基于JavaScript的生活游戏,并且我目前正在开发一种薪资系统,因此,例如,每10秒钟余额就会增加1。我一直在寻找方法,但是从来没有发现任何东西。这是我的代码:
var balance = 100;
function balanceSys(){
let newbalance = balance + 100;
balance = newbalance;
document.getElementById("balance").innerHTML = (balance);
}
function salary(){
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Life Game</title>
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Mukta+Malar" rel="stylesheet">
</head>
<script src="main.js"></script>
<body>
<center><h1 class="title">Life Game</h1></center>
<table>
<tr>
<td class="mainTd"><h1 id="balance">0</h1></td>
<td class="mainTd"><h1 id="state">Begger</h1></td>
</tr>
</table>
<button onclick="balanceSys()">Click Me</button>
</body>
</html>
答案 0 :(得分:0)
使用setInterval()
。它将每X毫秒(第二个参数)执行一次功能。
您还可以输入amt的参数进行调整,并添加不同的数量(在这种情况下,我在原始间隔函数中指定了1,如果未指定,则默认为100。
var balance = 0;
setInterval(() => balanceSys(1), 1000);
function balanceSys(amt = 100){
let newbalance = balance + amt;
balance = newbalance;
document.getElementById("balance").innerHTML = (balance);
}
function salary(){
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Life Game</title>
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Mukta+Malar" rel="stylesheet">
</head>
<script src="main.js"></script>
<body>
<center><h1 class="title">Life Game</h1></center>
<table>
<tr>
<td class="mainTd"><h1 id="balance">0</h1></td>
<td class="mainTd"><h1 id="state">Begger</h1></td>
</tr>
</table>
<button onclick="balanceSys()">Click Me</button>
</body>
</html>
答案 1 :(得分:0)
我同意FrankerZ的方法,但是您不想每秒运行function balanceSys(){
let element = document.getElementById("balance");
setInterval(() => {
let newbalance = balance + 1;
balance = newbalance;
element.innerHTML = (balance);
}, 10000);
}
。这是一个更好的解决方案。 DOM操作非常缓慢,因此请尝试尽可能降低对性能的影响。
func selection(from startPage: PDFPage, atCharacterIndex startCharacter: Int, to endPage: PDFPage, atCharacterIndex endCharacter: Int) -> PDFSelection?