简体   繁体   English

JavaScript:调用已在另一个函数中更改的变量?

[英]JavaScript: Calling a variable that has been altered within another function?

I'm trying to create an alert in JavaScript using cookies that both greets the user by name and tells them how many times they've won a specific game on the website. 我正在尝试使用Cookie创建JavaScript警报,该cookie既可以按名称向用户打招呼,又可以告诉他们他们在网站上赢得了特定游戏的次数。 I have the user greeting working fine, but I can't seem to get the counter to work correctly. 我的用户问候语可以正常工作,但是我似乎无法使计数器正常工作。 It involves calling variables that are altered by a previous function, and I think that's where my problem is, but I'm not sure how to fix that. 它涉及到调用由上一个函数更改的变量,我认为这就是我的问题所在,但是我不确定如何解决该问题。 (The variables are called 'cardOnePoints' and 'cardTwoPoints'.) Can anyone help me with that or tell me if there's anything else I'm doing wrong? (这些变量称为“ cardOnePoints”和“ cardTwoPoints”。)有人可以帮我吗,或者告诉我是否还有其他问题吗?

function getCookieOne(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
    x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
    x = x.replace(/^\s+|\s+$/g, "");
    if (x == c_name) {
        return unescape(y);
    }
}
}

function setCookieOne(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" +     exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}

var winnerCount = getCookieOne('gamesWonCount') || 0;

function calculateWinnerCount() {
if (cardOnePoints + cardTwoPoints === 21) {
    winnerCount = winnerCount + 1;
    setCookieOne("gamesWonCount", winnerCount, 365);
}
}

function checkCookie() {
var username = getCookieOne("username");

if (username != null && username != "") {
    alert("Sup " + username + "!!! Woah man, you've won " + winnerCount + " game(s)!!!");
} else {
    username = prompt("Please enter your name:", "");
    if (username != null && username != "") {
        setCookieOne("username", username, 365);
    }
}
}

You can see my entire code here: http://jsfiddle.net/hayleyelisa/9padf/2/ 您可以在这里看到我的整个代码: http : //jsfiddle.net/hayleyelisa/9padf/2/

You're declaring those variables within the context of a function. 您在函数的上下文中声明这些变量。 You'll only be able to access them within the scope of that function. 您将只能在该功能范围内访问它们。 You can declare them outside the scope of both functions and you can access them within both. 您可以在两个函数的范围之外声明它们,也可以在两个函数中访问它们。

Javascript variables can be accessed from within functions that they're declared outside of, but variables declared within a function cannot be accessed outside of those functions 可以从在外部声明的函数内部访问Javascript变量,但是在这些函数外部不能访问在函数内部声明的变量

So instead of 所以代替

function dealHand() {
    ...
    var cardOnePoints;
    var cardTwoPoints;
    ...
}

function calculateWinnerCount() {
if (cardOnePoints + cardTwoPoints === 21) {
    ...
}

you want 你要

var cardOnePoints;
var cardTwoPoints;

function dealHand() {
    ...
}

function calculateWinnerCount() {
if (cardOnePoints + cardTwoPoints === 21) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM