setInterval 函数不是 运行 使用 JavaScript ES6
setInterval function not running using JavaScript ES6
这让我如厕
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const runTimer = () => setInterval(randomInt, 100); // function to call randomInt every 100ms
video.style.opacity = runTimer(); // set the opacity at random every 100ms
我不知道这是 ES6 和使用隐式还是显式的问题 returns。 FWIW 我也尝试过使用 trad Javascript,比如
runTimer() {...}
或function runTimer() {...}
(以及其他功能,none似乎有效)。
这一定是我遗漏的简单内容吧?
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const runTimer = () => setInterval(randomInt, 100); // function to call randomInt every 100ms
video.style.opacity = runTimer(); // set the opacity at random every 100ms
video {
width: 100%;
}
<video class="video" poster="https://64.media.tumblr.com/tumblr_qsflcrfAoC1z0l4vz_smart1.jpg" preload="none" muted="" data-crt-video="" crossorigin="anonymous" tabindex="-1" loop controls>
<source src="https://va.media.tumblr.com/tumblr_qsflcrfAoC1z0l4vz_480.mp4" type="video/mp4">
</video>
该代码试图让 runTimer
在计时器间隔内调用它之前伸出并重用代码。函数做不到。
相反,为每个间隔回调移动您想要 运行 的所有逻辑,包括 video.style.opacity =
部分,inside 您传递给 setInterval
。查看 ***
条评论:
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const updateOpacity = () => video.style.opacity = randomInt(); // ***
const runTimer = () => setInterval(updateOpacity, 100); // function to call randomInt every 100ms
// *** −−−−−−−−−−−−−−−−−−−−−−−−−−−−^
runTimer(); // set the opacity at random every 100ms
这让我如厕
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const runTimer = () => setInterval(randomInt, 100); // function to call randomInt every 100ms
video.style.opacity = runTimer(); // set the opacity at random every 100ms
我不知道这是 ES6 和使用隐式还是显式的问题 returns。 FWIW 我也尝试过使用 trad Javascript,比如
runTimer() {...}
或function runTimer() {...}
(以及其他功能,none似乎有效)。
这一定是我遗漏的简单内容吧?
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const runTimer = () => setInterval(randomInt, 100); // function to call randomInt every 100ms
video.style.opacity = runTimer(); // set the opacity at random every 100ms
video {
width: 100%;
}
<video class="video" poster="https://64.media.tumblr.com/tumblr_qsflcrfAoC1z0l4vz_smart1.jpg" preload="none" muted="" data-crt-video="" crossorigin="anonymous" tabindex="-1" loop controls>
<source src="https://va.media.tumblr.com/tumblr_qsflcrfAoC1z0l4vz_480.mp4" type="video/mp4">
</video>
该代码试图让 runTimer
在计时器间隔内调用它之前伸出并重用代码。函数做不到。
相反,为每个间隔回调移动您想要 运行 的所有逻辑,包括 video.style.opacity =
部分,inside 您传递给 setInterval
。查看 ***
条评论:
const video = document.querySelector('.video'); // grab the element
const randomInt = () => Math.random().toFixed(2); // get random int from 0 - 1 and round to two decimal points
const updateOpacity = () => video.style.opacity = randomInt(); // ***
const runTimer = () => setInterval(updateOpacity, 100); // function to call randomInt every 100ms
// *** −−−−−−−−−−−−−−−−−−−−−−−−−−−−^
runTimer(); // set the opacity at random every 100ms