Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的Javascript时钟变量分配_Javascript_Variable Assignment - Fatal编程技术网

简单的Javascript时钟变量分配

简单的Javascript时钟变量分配,javascript,variable-assignment,Javascript,Variable Assignment,以下带注释的javascript代码创建了一个简单的计时器,为了MWE的缘故,它出现在控制台日志中。我试图解释这段代码的每一点,但我被第2节和第10节中的作业细节所困扰。我谦恭地请求帮助填写缺失的部分 //1-Create a function "startTimer" with parameter "duration" function startTimer(duration) { //2-Declare a variable "timer" with some strange comma

以下带注释的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
}
//1-创建一个带有参数“duration”的函数“startTimer”
功能启动计时器(持续时间){
//2-声明一个带有奇怪逗号分隔赋值的变量“timer”(此处有帮助)
var定时器=持续时间,分钟,秒;
//3-声明未赋值变量“输出”
var输出;
//4-setInterval,语法为*setInterval(函数、毫秒、参数1、参数2等)*,表示以下代码将每隔1000毫秒或每1秒执行一次。
setInterval(函数(){
//5-为变量“分钟”分配计时器/60的值,基数为10。
分钟=parseInt(计时器/60,10);
//6-为变量“秒”分配定时器mod 60的值,基数为10。
秒=parseInt(计时器%60,10);
//7-如果分钟数小于10,则将变量“分钟数”赋值为“0”+分钟数,如果不是,则将变量“分钟数”赋值为分钟数。这是通过三元运算符“?”完成的,该运算符具有语法*条件?exprT:exprF*
分钟=分钟<10?“0”+分钟:分钟;
//8-以与分配分钟相同的方式分配变量“秒”,如果值为第2节,则会添加前导零
var定时器=持续时间,分,秒;
这一行声明了3个变量:
timer
minutes
seconds

它还将变量
定时器
初始化为
持续时间
的值

第10节
if(--timer<0){
定时器=持续时间;
}
--timer
语法意味着首先将timer的值减少1,然后读取其值以进行
if(timer<0)
比较。
duration
变量是
startTimer
函数的一个参数,因此其值由函数的用户在该代码的范围之外赋值。

第2节
var定时器=持续时间,分,秒;
这一行声明了3个变量:
timer
minutes
seconds

它还将变量
定时器
初始化为
持续时间
的值

第10节
if(--timer<0){
定时器=持续时间;
}
--timer
语法意味着首先将timer的值减少1,然后读取其值以进行
if(timer<0)
比较。

duration
变量是
startTimer
函数的一个参数,因此它的值由函数的用户在本代码范围之外赋值。

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

//1-创建一个带有参数“duration”的函数“startTimer”
功能启动计时器(持续时间){
//2-声明一个变量“timer”,并为其指定参数“duration”的值。
var定时器=持续时间;
//3-声明未分配变量“输出”、“分钟”和“秒”。
var输出;
var分钟;
var秒;
//4-setInterval,语法为*setInterval(函数、毫秒、参数1、参数2等)*。对于第12节,以下代码将每隔1000毫秒或每1秒执行一次。
setInterval(函数(){
//5-以基数(基数)10为变量“分钟”分配定时器/60的值。
分钟=parseInt(计时器/60,10);
//6-为变量“秒”分配基数为10的定时器mod 60的值。
秒=parseInt(计时器%60,10);
//7-如果分钟数小于10,则将变量“分钟数”赋值为“0”+分钟数,如果不是,则将变量“分钟数”赋值为分钟数。这是通过三元运算符“?”完成的,该运算符具有语法*条件?exprT:exprF*
分钟=分钟<10?“0”+分钟:分钟;

//8-以分配分钟的相同方式分配变量“seconds”,如果值为,则会添加前导零。为了完整性,这是一个更新版本,有完整的解释。此代码完全包含在javascript中,没有任何HTML等(例如,在code.org app studio上)

//1-创建一个带有参数“duration”的函数“startTimer”
功能启动计时器(持续时间){
//2-声明一个变量“timer”,并为其指定参数“duration”的值。
var定时器=持续时间;
//3-声明未分配变量“输出”、“分钟”和“秒”。
var输出;
var分钟;
var秒;
//4-setInterval,语法为*setInterval(函数、毫秒、参数1、参数2等)*。对于第12节,以下代码将每隔1000毫秒或每1秒执行一次。
setInterval(函数(){
//5-以基数(基数)10为变量“分钟”分配定时器/60的值。
分钟=parseInt(计时器/60,10);
//6-为变量“秒”分配基数为10的定时器mod 60的值。
秒=parseInt(计时器%60,10);
//7-如果分钟数小于10,则将变量“分钟数”赋值为“0”+分钟数,如果不是,则将变量“分钟数”赋值为分钟数。这是通过三元运算符“?”完成的,该运算符具有语法*条件?exprT:exprF*
分钟=分钟<10?“0”+分钟:分钟;

//8-以分配分钟的相同方式分配变量“seconds”,如果值为Nr,则添加前导零。2是一种非常奇怪的写入方式,
var timer=duration;var minutes=undefined;var seconds=undefined;
向下递增
=
递减
Nr 10,
if(--timer<0)
timer-=1;if(timer<0)
相同,而
if(timer--<0)
var tmp=timer;timer-=1;if(tmp<0)相同
nr2是一种非常奇怪的书写方式,
var timer=duration;var minutes=undefined;var seconds=undefined;
向下递增
==
递减
nr10,
如果(--timer<0)
定时器-=1相同;如果
//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);