【问题标题】:Simple Javascript Clock Variable Assignment简单的 Javascript 时钟变量赋值
【发布时间】:2019-04-16 15:21:30
【问题描述】:

以下带注释的 javascript 代码创建了一个简单的计时器,为了 MWE,它出现在控制台日志中。我试图解释这段代码的每一点,但我被困在第 2 节和第 10 节中的作业细节上。我谦虚地请求帮助填写缺失的部分。

//1-Create a function "startTimer" with parameter "duration"
function startTimer(duration) {

//2-Declare a variable "timer" with some strange comma separated assignment (HELP HERE)
var timer = duration, minutes, seconds;

//3-Declare an unassigned variable "output"
var output;

//4-setInterval, which has the syntax *setInterval(function, milliseconds, param1, param2, ...)* indicated that the code below will be performed every interval of 1000 ms, or every one second.
setInterval(function () {

//5-Assign variable "minutes" the value of timer/60 and has radix (base) 10.
minutes = parseInt(timer / 60, 10);

//6-Assign variable "seconds" the value of timer mod 60 and has radix (base) 10.
seconds = parseInt(timer % 60, 10);

//7-Assign variable "minutes" to the value of "0" + minutes if minutes < 10, or minutes if not. This is accomplished with the ternary operator "?", which has syntax *condition ? exprT : exprF* 
minutes = minutes < 10 ? "0" + minutes : minutes;

//8-Assign variable "seconds" in the same way minutes are assigned, which adds a leading zero if the value is <10.
seconds = seconds < 10 ? "0" + seconds : seconds;

//9-Assign variable "output" to minutes concatenated with ":" concatenated with seconds
output = minutes + ":" + seconds;

//10-If the value of timer incremented downward by one (HELP ON WHAT THAT MEANS) is less than zero then assign variable "timer" to the value of variable "duration", which will be (HELP ON WHERE DURATION GETS ITS VALUE).
if (--timer < 0) {
timer = duration;
}

//11-Output the formatted timer to the console log
console.log(output);

//12-End the function performed in setInterval. Also set the interval in which the function is repeated to 1000 ms (the second argument in the setInterval function).
}, 1000);

//13-End of function startTimer
}

【问题讨论】:

  • Nr 2 是一种非常奇怪的写作方式,var timer = duration; var minutes = undefined; var seconds = undefined;
  • incremented downward === decremented
  • 编号10,if(--timer &lt; 0)timer -= 1; if (timer &lt; 0)相同,而if(timer-- &lt; 0)var tmp = timer; timer -= 1; if (tmp &lt; 0)相同

标签: javascript variable-assignment


【解决方案1】:

第 2 节

var timer = duration, minutes, seconds;

这一行声明了 3 个变量:timerminutesseconds
它还将变量timer 初始化为duration 的值。

第 10 节

if (--timer < 0) {
timer = duration;
}

--timer 语法意味着计时器的值先减 1,然后读取它的值以进行if (timer &lt; 0) 比较。
duration 变量是 startTimer 函数的一个参数,因此它的值是由函数的用户在此代码范围之外分配的。

【讨论】:

  • 澄清第 10 节的答案,创建具有相同值的 var a,b 和 console.log(a++) & console.log(++b)
【解决方案2】:

为了完整起见,这是一个带有完整解释的更新版本。此代码完全包含在 javascript 中,没有任何 HTML 等(例如,在 code.org 应用工作室)。

//1-Create a function "startTimer" with parameter "duration"
function startTimer(duration) {

//2-Declare a variable "timer" and assign it the value of the parameter "duration".
var timer = duration;


//3-Declare unassigned variables "output", "minutes", and "seconds".
var output;
var minutes;
var seconds;

//4-setInterval, which has the syntax *setInterval(function, milliseconds, param1, param2, ...)*. With section 12, this code below will be performed every interval of 1000 ms, or every one second.
setInterval(function () {

//5-Assign variable "minutes" the value of timer/60 with radix (base) 10.
minutes = parseInt(timer / 60, 10);

//6-Assign variable "seconds" the value of timer mod 60 with radix (base) 10.
seconds = parseInt(timer % 60, 10);

//7-Assign variable "minutes" to the value of "0" + minutes if minutes < 10, or minutes if not. This is accomplished with the ternary operator "?", which has syntax *condition ? exprT : exprF* 
minutes = minutes < 10 ? "0" + minutes : minutes;

//8-Assign variable "seconds" in the same way minutes are assigned, which adds a leading zero if the value is <10.
seconds = seconds < 10 ? "0" + seconds : seconds;

//9-Assign variable "output" to minutes concatenated with ":" concatenated with seconds
output = minutes + ":" + seconds;

//10-Decrement (lower) the value of timer by one first (since -- is in front), THEN check if that value is <0. If so, assign variable "timer" to the value of variable "duration", which was initially set by a parameter at the beginning of the setTimer function. This causes the timer to loop back to the initial duration once the timer passes zero. You could put different code here if you want a different result after the timer reaches zero.
if (--timer < 0) {
timer = duration;
//if you want to stop the function entirely when the function passes zero, uncomment the next line to end the function by returning nothing.
//return;
}

//11-Output the formatted timer to the console log. You could also use something like, "setText("id",output);" to set a UI element to the value of the timer.
console.log(output);

//12-End the function performed in setInterval. Also set the interval in which the function is repeated to 1000 ms (the second argument in the setInterval function).
}, 1000);

//13-End of function startTimer
}

//14-Start the timer by calling function "startTimer" with a 30 second duration
startTimer(30);

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2014-03-22
    • 2017-07-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多