首页 > 解决方案 > this.function 不是函数,但我可以在其他方法上访问此方法

问题描述

我在getTestContent函数中执行submitMyAnswer时遇到问题。我认为这与 clearIntervals 有关,但事实并非如此。我尝试将“submitMyAnswer”函数的全部内容添加到我的主函数中,但它给了我一个错误。我应该怎么办?我应该尝试“返回”该方法吗?cannot read property 'dispatch' of undefined

//other method is called submitMyAnswer
getTestContent() {
    this.gotEm2 = true;
    this.gotEm = false;
    localStorage.getItem("savedId");
    axios
        .get("https://www.derptech.site/MyComposer/", {
            params: {
                id: 8,
                testToken: this.selectedTest,
            },
        })
        .then((res) => {
            for (var l = 0; l < res.data.length; l++) {
                this.quizTime = res.data[l].Duration;
                this.testContentData.push({
                    testId: res.data[l].TestId,
                    daimage: res.data[l].DaImage,
                    question: res.data[l].Question,
                });
            }

            var totalSeconds;

            if (this.quizTime != 0) {

                if (localStorage.getItem("totalSeconds")) {
                    totalSeconds = localStorage.getItem("totalSeconds");
                } else {
                    totalSeconds = this.quizTime;
                }

                var minutesLabel = document.getElementById("minutes");
                var secondsLabel = document.getElementById("seconds");
                var startThisTime = setInterval(setTime, 1000);
                //setInterval(setTime, 1000);

                function setTime() {
                    --totalSeconds;
                    secondsLabel.innerHTML = pad(totalSeconds % 60);
                    minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
                    localStorage.setItem("totalSeconds", totalSeconds);


                    if (totalSeconds <= 0) {
                        clearInterval(startThisTime);
                        this.submitMyAnswer();
                    }
                }


                function pad(val) {
                    var valString = val + "";
                    if (valString.length < 2) {
                        return "0" + valString;
                    } else {
                        return valString;
                    }
                }

                window.addEventListener("beforeunload", function(e) {
                    e.preventDefault();
                    s;
                    e.returnValue = "";
                });
            }

        });
},

标签: javascriptvue.js

解决方案


推荐阅读