这个功能只工作一次是有原因的吗?
Is there a reason why this function only works once?
我正在使用 JS-Cookie 制作一个简单的唱首歌游戏。当我调用该函数时,它只工作一次,但随后我必须刷新才能使该函数再次工作。我尝试在它后面添加一个 console.log 以帮助调试它,并且显示了日志,但是 Cookies.set 不起作用。我不知道发生了什么,而且我对这个库不太熟悉,所以非常感谢任何帮助。
Javascript
(function refreshCookies(i) {
setTimeout(function () {
let flame = Cookies.get('flame');
let gems = Cookies.get('gems');
let cuid = Cookies.get('CUID');
let username = Cookies.get('username');
let value = Cookies.get('value');
let value_amount = document.getElementById('value-amount');
let flame_amount = document.getElementById('flame-amount');
let gem_amount = document.getElementById('gem-amount');
let board_placing = document.getElementById('board-placing');
let welcome_username = document.getElementById('welcome-username');
let username_header = document.getElementById('username-header');
let title = document.getElementById('title');
value_amount.innerHTML = "$" + value;
flame_amount.innerHTML = "<i class=\"fas fa-fire-alt\"></i> " + flame;
gem_amount.innerHTML = "<i class=\"fas fa-gem\"></i> " + gems;
welcome_username.innerHTML = "Welcome, " + username + "!";
username_header.innerHTML = username;
title.innerHTML = "Shoe Clicker - $" + value;
if (--i) {
refreshCookies(i);
}
}, 0500);
})(Infinity);
// clicker
function addClick() {
Cookies.set('value', parseInt(value) + 1);
console.log('click added!');
}
HTML
<section onclick="addClick();" class="clicker">
<h2>Press anywhere to get money!<h2>
<i class="fas fa-dollar-sign"></i>
</section>
使用增量增加每次点击的价值。一个简单的演示在下面的片段中。
var money = 0
function addClick() {
money++
document.getElementById("demo").innerHTML = money
}
<section onclick="addClick();" class="clicker">
<h2>Press on me to get money $ !</h2>
<i class="fas fa-dollar-sign"></i>
</section>
<div id="demo"></div>
你试过添加这样的东西吗?
function addClick() {
let value = Cookies.get('value');
Cookies.set('value', parseInt(value) + 1);
console.log('click added!');
}
我正在使用 JS-Cookie 制作一个简单的唱首歌游戏。当我调用该函数时,它只工作一次,但随后我必须刷新才能使该函数再次工作。我尝试在它后面添加一个 console.log 以帮助调试它,并且显示了日志,但是 Cookies.set 不起作用。我不知道发生了什么,而且我对这个库不太熟悉,所以非常感谢任何帮助。
Javascript
(function refreshCookies(i) {
setTimeout(function () {
let flame = Cookies.get('flame');
let gems = Cookies.get('gems');
let cuid = Cookies.get('CUID');
let username = Cookies.get('username');
let value = Cookies.get('value');
let value_amount = document.getElementById('value-amount');
let flame_amount = document.getElementById('flame-amount');
let gem_amount = document.getElementById('gem-amount');
let board_placing = document.getElementById('board-placing');
let welcome_username = document.getElementById('welcome-username');
let username_header = document.getElementById('username-header');
let title = document.getElementById('title');
value_amount.innerHTML = "$" + value;
flame_amount.innerHTML = "<i class=\"fas fa-fire-alt\"></i> " + flame;
gem_amount.innerHTML = "<i class=\"fas fa-gem\"></i> " + gems;
welcome_username.innerHTML = "Welcome, " + username + "!";
username_header.innerHTML = username;
title.innerHTML = "Shoe Clicker - $" + value;
if (--i) {
refreshCookies(i);
}
}, 0500);
})(Infinity);
// clicker
function addClick() {
Cookies.set('value', parseInt(value) + 1);
console.log('click added!');
}
HTML
<section onclick="addClick();" class="clicker">
<h2>Press anywhere to get money!<h2>
<i class="fas fa-dollar-sign"></i>
</section>
使用增量增加每次点击的价值。一个简单的演示在下面的片段中。
var money = 0
function addClick() {
money++
document.getElementById("demo").innerHTML = money
}
<section onclick="addClick();" class="clicker">
<h2>Press on me to get money $ !</h2>
<i class="fas fa-dollar-sign"></i>
</section>
<div id="demo"></div>
你试过添加这样的东西吗?
function addClick() {
let value = Cookies.get('value');
Cookies.set('value', parseInt(value) + 1);
console.log('click added!');
}