Javascript:了解未给出参数的函数中参数的含义和行为

标签 javascript function

<分区>

我很抱歉打扰你,我怀疑除了我自己之外的任何人都认为这是微不足道的问题。我尽力自己寻找答案,但无法找到。

为了让您了解我不了解的地方,我将为您提供一个取自 answer 的示例至 another question在这里问。

我在这个例子的上下文中的问题:newtime 的行为是什么?为什么需要将其定义为参数才能使此功能起作用?它从哪里获得它的值(value)?

startAnimating(5);

function startAnimating(fps) {
    fpsInterval = 1000 / fps;
    then = window.performance.now();
    startTime = then;
    console.log(startTime);
    animate();
}

function animate(newtime) {
    // (...)
    requestAnimationFrame(animate);
    now = newtime;
    elapsed = now - then;

    if (elapsed > fpsInterval) {
        then = now - (elapsed % fpsInterval);
        // (...)
    }
}

我将不胜感激任何回应。谢谢。

最佳答案

Where does it get its value

requestAnimationFrame 将在未来使用传入的函数调用您的函数,您需要该值来计算耗时。

callback

A parameter specifying a function to call when it's time to update your animation for the next repaint. The callback has one single argument, a DOMHighResTimeStamp, which indicates the current time (the time returned from performance.now() ) for when requestAnimationFrame() starts to fire callbacks.

引用:https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame#Parameters

关于Javascript:了解未给出参数的函数中参数的含义和行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969918/

相关文章:

javascript - Google Closure 库的基准测试

c - 在 C 中实现通用交换宏

javascript - Titanium 将文本字段值传递到新窗口

Swift函数只能调用一次

javascript - 调用另一个函数时如何禁用该函数?

javascript - jquery qrcode 无法使用超过 34 个字符长

javascript - Highcharts 比较日期范围条形图

javascript - 单击时,如果有类则发出警报,否则添加类问题

javascript - 从 webforms 项目中的 javascript 文件调用 mvc Controller

javascript - 简单的 add 函数返回 NaN