Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.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 功能“;设置秒()”;不适用于p5.js web编辑器_Javascript_Date_P5.js_Seconds - Fatal编程技术网

Javascript 功能“;设置秒()”;不适用于p5.js web编辑器

Javascript 功能“;设置秒()”;不适用于p5.js web编辑器,javascript,date,p5.js,seconds,Javascript,Date,P5.js,Seconds,正如您所料,setSeconds();通常会返回当前日期的秒数,并将其设置为参数内的指定值。但是无论我在p5.js web编辑器中尝试什么,它都绝对没有效果 我想编写一些代码来计算移动对象的角度(从水平方向)和方向。尝试在我的代码中可以想象的任何地方插入函数,甚至将我的代码片段与其他代码片段隔离,以确保没有不需要的外部代码(如其他变量)影响它 function seconds_Passed() { timePassed = new Date() timePassed.setSeco

正如您所料,setSeconds();通常会返回当前日期的秒数,并将其设置为参数内的指定值。但是无论我在p5.js web编辑器中尝试什么,它都绝对没有效果

我想编写一些代码来计算移动对象的角度(从水平方向)和方向。尝试在我的代码中可以想象的任何地方插入函数,甚至将我的代码片段与其他代码片段隔离,以确保没有不需要的外部代码(如其他变量)影响它

function seconds_Passed()
{  
  timePassed = new Date() 
  timePassed.setSeconds(0); //Has no effect even though many js examples show 
                            //this as the way to do it.
  secondsPassed = timePassed.getSeconds();
  console.log(secondsPassed);
}
没有错误消息。 预期的结果是:当我运行代码时,秒总是从0开始,而不是您在桌面时钟上看到的实际实时秒数。

setSeconds()
不是p5.js函数,这是一个标准的js方法,根据本地时间设置指定日期的秒数

因此,解析:-

function seconds_Passed()
{  
  timePassed = new Date() 
  timePassed.setSeconds(0); //Has no effect even though many js examples show 
                            //this as the way to do it.
  secondsPassed = timePassed.getSeconds();
  console.log(secondsPassed);
}
将始终在几秒钟内返回0

如果需要获取两个事件之间的时间,则需要捕获两个时间戳

例如:-

function seconds_Passed()
{  
const date=新日期();
const start=date.getTime();
log(“启动UNIX时间”+启动);
设置间隔(()=>{
const nowDate=新日期();
const timeParsed=(Math.round((nowDate.getTime()-start)/1000));
log(timeParsed+“脚本启动后解析的秒数”);
}, 3000)
}

秒数已过()
对不起,我不明白你的意思。方法设置日期对象的秒数。我对它进行了测试,它开始工作了。您希望通过此功能实现什么@戴维迪对此表示感谢。但是,您能否进一步说明为什么web编辑器不包含该函数?我偶然发现这个编辑器,假设它基本上是Javascript?如果不是,那是什么?如果它没有一些标准的Javascript函数,那么为什么它被称为“p5.js”呢?我刚刚在p5.js中测试了您的代码,它完全按照预期工作。