gpt4 book ai didi

javascript - 时间戳的值从哪里来?

转载 作者:行者123 更新时间:2023-12-02 23:10:23 27 4
gpt4 key购买 nike

下面的代码片段来自 MDN ( https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/loops-and-intervals/simple-raf-spinner.html )。我就是不明白函数draw()的参数'timestamp',这个值是从哪里来的?

我尝试在控制台中检查它,rotateCount计算正确,startTime也可以,但时间戳未定义。

<script>
// Store reference to the div element, create a rotate counter and null startTime
// and create an uninitialized variable to store the requestAnimationFrame() call in
const spinner = document.querySelector('div');
let rotateCount = 0;
let startTime = null;
let rAF;
// Create a draw() function
function draw(timestamp) {
if(!startTime) {
startTime = timestamp;
}
rotateCount = (timestamp - startTime) / 3;

// If rotateCount gets over 359, set it to 'remainder of dividing by 360'
if(rotateCount > 359) {
rotateCount %= 360;
}
// Set the rotation of the div to be equal to rotateCount degrees
spinner.style.transform = 'rotate(' + rotateCount + 'deg)';

// Call the next frame in the animation
rAF = requestAnimationFrame(draw);
}
draw();
</script>

最佳答案

当您在 draw() 函数内使用 console.log(timestamp) 时,您将看到第一次调用时时间戳未定义,但之后的所有调用它具有值(value)。

这是因为 draw() 函数中的最后一行将 draw 作为回调函数传递给 requestAnimationFrame()功能。

requestAnimationFrame()内部调用draw()并传递时间戳参数。

关于javascript - 时间戳的值从哪里来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57397574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com