Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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 - Fatal编程技术网

JavaScript中的秒表-输出不正确

JavaScript中的秒表-输出不正确,javascript,Javascript,请有人帮我理解为什么这个项目的产出是: 停在:NaN 而不是预期的产出,即: 停在:3 多谢各位 这段代码是用JSES6编写的 功能秒表(){ 此值为0.watchtime=0; 此值为0; this.start=函数(){ 如果(此.started){ d=新日期() console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000) }else if(this.watchtime==0){ 此参数=1 d=新日期() this.w

请有人帮我理解为什么这个项目的产出是:

停在:NaN

而不是预期的产出,即:

停在:3

多谢各位

这段代码是用JSES6编写的

功能秒表(){
此值为0.watchtime=0;
此值为0;
this.start=函数(){
如果(此.started){
d=新日期()
console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000)
}else if(this.watchtime==0){
此参数=1
d=新日期()
this.watchtime=d.getTime()
}否则{
d=新日期()
log(“秒表从重新启动”,(d.getTime()-this.watchtime)/1000)
this.watchtime=d.getTime()-this.watchtime
此参数=1
}
}
this.stop=函数(){
如果(this.started==0){
console.log(“秒表尚未启动”)
}否则{
d=新日期()
this.total_time=(d.getTime()-this.watchtime)/1000
console.log(“停止于:”,此.total_时间)
此参数为0
}
}
}
sw=新秒表
sw.start()

setTimeout(sw.stop,3000)
存在参考问题,已更正代码

功能秒表(){
var me=这个;
此值为0.watchtime=0;
此值为0;
this.start=函数(){
如果(此.started){
d=新日期()
console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000)
}else if(this.watchtime==0){
此参数=1
d=新日期()
this.watchtime=d.getTime()
}
否则{
d=新日期()
log(“秒表从重新启动”,(d.getTime()-this.watchtime)/1000)
this.watchtime=d.getTime()-this.watchtime
此参数=1
}
}
this.stop=函数(){
如果(this.started==0){
console.log(“秒表尚未启动”)
}否则{
d=新日期()
this.total_time=(d.getTime()-me.watchtime)/1000
console.log(“停止于:”,此.total_时间)
此参数为0
}
}
}
sw=新秒表;
sw.start();

设置超时(开关停止,3000)存在参考问题,已更正代码

功能秒表(){
var me=这个;
此值为0.watchtime=0;
此值为0;
this.start=函数(){
如果(此.started){
d=新日期()
console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000)
}else if(this.watchtime==0){
此参数=1
d=新日期()
this.watchtime=d.getTime()
}
否则{
d=新日期()
log(“秒表从重新启动”,(d.getTime()-this.watchtime)/1000)
this.watchtime=d.getTime()-this.watchtime
此参数=1
}
}
this.stop=函数(){
如果(this.started==0){
console.log(“秒表尚未启动”)
}否则{
d=新日期()
this.total_time=(d.getTime()-me.watchtime)/1000
console.log(“停止于:”,此.total_时间)
此参数为0
}
}
}
sw=新秒表;
sw.start();

设置超时(开关停止,3000)当您使用ES6时,使用箭头函数它将解决参考问题

功能秒表(){
此值为0.watchtime=0;
此值为0;
this.start=()=>{
如果(此.started){
d=新日期()
console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000)
}else if(this.watchtime==0){
此参数=1
d=新日期()
this.watchtime=d.getTime()
}否则{
d=新日期()
log(“秒表从重新启动”,(d.getTime()-this.watchtime)/1000)
this.watchtime=d.getTime()-this.watchtime
此参数=1
}
}
this.stop=()=>{
如果(this.started==0){
console.log(“秒表尚未启动”)
}否则{
d=新日期()
this.total_time=(d.getTime()-this.watchtime)/1000
console.log(“停止于:”,此.total_时间)
此参数为0
}
}
}
sw=新秒表
sw.start()

setTimeout(sw.stop,3000)
当您使用ES6时,使用箭头函数,它将解决参考问题

功能秒表(){
此值为0.watchtime=0;
此值为0;
this.start=()=>{
如果(此.started){
d=新日期()
console.log(“无法启动监视两次。当前时间为”,(d.getTime()-this.watchtime)/1000)
}else if(this.watchtime==0){
此参数=1
d=新日期()
this.watchtime=d.getTime()
}否则{
d=新日期()
log(“秒表从重新启动”,(d.getTime()-this.watchtime)/1000)
this.watchtime=d.getTime()-this.watchtime
此参数=1
}
}
this.stop=()=>{
如果(this.started==0){
console.log(“秒表尚未启动”)
}否则{
d=新日期()
this.total_time=(d.getTime()-this.watchtime)/1000
console.log(“停止于:”,此.total_时间)
此参数为0
}
}
}
sw=新秒表
sw.start()

设置超时(sw.stop,3000)
谢谢。这是我第一天使用JavaScript,所以很高兴了解JavaScript的概念谢谢。这是我第一天使用JavaScript,所以很高兴了解JavaScript的概念谢谢。这是我第一天使用JavaScript,所以很好地了解了我已经了解的箭头函数的概念,但我仍然不明白为什么这样解决了这个问题。请你详细说明一下,或者给我一个链接来解释一下?谢谢我注意到第一个函数(start)不需要箭头函数。因此,我很困惑,如果我