加载脚本时,我的全局变量会不断刷新,有没有办法阻止它?

时间:2015-11-29 21:43:45

标签: javascript jquery html

我正在尝试为系统完成原型,但是当我重新加载脚本时,我无法找到一种方法来停止我的全局变量初始化。当我正在编写我的完整系统时,我将使用localStorage等,但是现在我只是想找到一个快速修复来阻止变量和函数在每次加载脚本时执行。

$(function() {
    questions = [];
    count = 0;
    addQuestions();
    var originalHtml = document.body.innerHTML;
    $(document).ready(createWebpage);
    $(document).ready(createAnswerPage);

    function Question(questionText, possibleAnswers, chosenAnswer) {
        this.questionText = questionText;
        this.possibleAnswers = possibleAnswers;
        this.chosenAnswer = chosenAnswer;
    }

    function addQuestions() {
        var question1 = new Question(
            "Do you have or are you being treated for high blood pressure?",
            ['Yes','No'],
            ""
        );

        var question2 = new Question(
            "Within the last year, have you had chest pain or pressure with activity such as walking or climbing stairs?",
            ['Yes','No'],
            ""
        );

        var question3 = new Question(
            "Do you currently take medication to prevent or reduce agnia (chest pain from the heart)?",
            ['Yes','No'],
            ""
        );

        var question4 = new Question(
            "Have you ever had a heart attack?",
            ['Yes', 'No'],
            ""
        );

        questions.push(question1);
        questions.push(question2);
        questions.push(question3);
        questions.push(question4);
    }

    function submitFunction() {

        if ($('input[name=answerValue]:checked', '#buttons').val() != null) {
            questions[count].chosenAnswer = $('input[name=answerValue]:checked', '#buttons').val();
            console.log( questions[count].chosenAnswer);
            count++;
            if (count < questions.length) {
                document.body.innerHTML = originalHtml;
                createWebpage();
                } else {
                location.href= '../src/answerpage.html';
            }
        } else {
            alert("Please select an option");
        }
    }

    function createAnswerPage() {

        for (var i = 0; i < questions.length; i++) {
            $('#question_section').append("<h1>" + questions[i].questionText + "</h1>");
            $('#answer_section').append("<h1>" + questions[i].chosenAnswer + "</h1>");
            console.log(questions[i].chosenAnswer);
        }

    }

    function createWebpage() {


        $('#question_text').append("<h1>" + questions[count].questionText + "</h1>");
        console.log("questions");
        for ( var index = 0; index < questions[count].possibleAnswers.length; index++) {
            console.log(questions[0].possibleAnswers[index]);
            $('#buttons').append("<input type='radio' name = 'answerValue'  value=" + questions[count].possibleAnswers[index] + ">" + questions[count].possibleAnswers[index] + "<br>");
        }


        $('#answer_text').append("<br> <br> <br>");
        $('#answer_text').append("<button id = 'nextPage'>Next</button>");
        $('#nextPage').bind("click",submitFunction);



    }

});

在我添加新网页的提交功能中,我试图在脚本重新加载时使用问题[i] .chosenAnswer,但每次运行脚本时都会让我运行addQuestions()函数用原始数据替换我的数组中的所有对象。

1 个答案:

答案 0 :(得分:2)

首先,请阅读Why are global variables considered bad practice?

<小时/> 在您的情况下,快速解决方法是使用HTML5 localStorage

实施例

window.localStorage.setItem('myCat', 'Tom'); // save data
window.localStorage.getItem('myCat'); // get data

在这里,您使用myCat作为键/变量。

Window.localStorage - MDN docs