简单的Javascript时钟变量分配

时间:2019-04-16 15:21:31

标签: javascript variable-assignment

以下带注释的javascript代码创建了一个简单的计时器,出于MWE的考虑,它会出现在控制台日志中。我试图解释这段代码的每一部分,但是我在第2部分和第10部分中停留在分配的详细信息上。我衷心要求提供帮助以填补缺失的部分。

//we have a parameter

uint8_t num_8 = 4;

//we have a function

void doSomething(uint32_t num_32) {....}

//we call the function and passing the parameter

doSomething(num_8);

2 个答案:

答案 0 :(得分:2)

第2节

var timer = duration, minutes, seconds;

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

第10

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

--timer语法意味着先将timer的值减1 ,然后再读取其值以进行if (timer < 0)比较。
duration变量是startTimer函数的参数,因此,函数的用户将其值分配在此代码的范围之外。

答案 1 :(得分:0)

为完整起见,这是具有完整说明的更新版本。此代码可以完全包含在javascript中,并且没有任何HTML等(例如,在code.org app studio上)。

//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);