简体   繁体   English

步速功能参数

[英]Pace function parameter

I've created my function that will fetch for my API to change the user's pace. 我已经创建了函数,该函数将为我的API提取以更改用户的进度。 In my changePace function, i've established my parameter pace, and that parameter will vary depending on which pace is selected by the user. 在我的changePace函数中,我已经建立了我的参数步速,该参数将根据用户选择的步速而变化。 I have 4 paces, Steady, Strenuous, Grueling and Resting. 我有4个步伐,稳定,剧烈,痛苦和休息。 Any of which could be my parameter for the changePace function. 其中任何一个都可以作为changePace函数的参数。

But when I try to call my changePace function with the appropriate parameter, I'm getting the following error. 但是,当我尝试使用适当的参数调用changePace函数时,出现以下错误。

trail.js:8 Uncaught ReferenceError: steady is not defined
    at HTMLBodyElement.document.body.onkeyup 

JS FILE : JS文件:

document.body.onkeyup = function(e){
    if(e.keyCode == 13){
        document.getElementById("paces").style.color = "white";
        paceDiv = true;
        console.log("Works");
    }
    if(e.keyCode == 49 && paceDiv == true) {
      changePace(steady);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 50 && paceDiv == true){
      changePace(strenuous);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 51 && paceDiv == true){
      changePace(grueling);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 52 && paceDiv == true){
      changePace(resting);
      document.getElementById("paces").style.color = "black";
    }
    if(e.keyCode == 32) {
      changeDay();
      document.getElementById("paces").style.color = "black";
    }
}

function changePace(pace) {
    fetch('/api/changePace',
        {method: "post",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: '{"pace": "' + pace + '"}'
        })
    .then(function(response) {
        if (response.status !== 200) {
            console.log('Error: ' + response.status + "..." + response.value);
            return;
        }
        response.json().then(function(data) {
            changeDay();
        });
    });
}

I believe you intended steady to be a string, but you've written it as a variable, which you never defined. 我相信您打算steady地成为一个字符串,但是您已经将它编写为一个从未定义的变量。

Try changePace('steady'); 尝试changePace('steady'); instead. 代替。

Or, alternatively, you could define the variables somewhere, maybe near the top of the file. 或者,也可以在文件顶部附近的某个位置定义变量。 This is the recommended approach if you're going to use the 'steady' string in more than one place. 如果要在多个位置使用'steady'字符串,则建议使用此方法。

var steady = 'steady';
var strenuous = 'strenuous';
var grueling = 'grueling';
var resting = 'resting';

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

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