gpt4 book ai didi

javascript - settimeout 调用的函数未结束

转载 作者:行者123 更新时间:2023-11-28 09:42:08 26 4
gpt4 key购买 nike

问题:在幻灯片放映中,下面的淡入淡出函数不断调用淡入/淡出函数,请使用 JsFiddle 并运行大约十秒来查看问题。在 IE 中不起作用,不要让 jsfiddle 运行太久,它可能会使您的浏览器崩溃!

JsFiddle:http://jsfiddle.net/HdYmH/

详细信息(对于那些感兴趣的人):嗨,很抱歉发布一个包含如此大代码块的问题。我仍在学习 javascript,并试图弄清楚如何制作幻灯片。我知道有很多 js 幻灯片,但我想将其作为一种学习体验。因此请注意,这段代码的某些部分非常糟糕。该问题可能与幻灯片的changeSlide()方法有关。

我使用 firebug 来找出哪个方法被调用最多,显然几秒后 fadeOut 将被调用 20k+ 次:|

// Generic fade function that fades in or out
function fade(pElem, pStartOpac, pEndOpac, fps, sec) {
if ((typeof (pElem) !== "string") || (typeof (pStartOpac) !== "number")
|| (typeof (pEndOpac) !== "number") || (typeof (fps) !== "number")
|| (typeof (sec) !== "number")) {
console.log("Parameters incorrect format has to be (string) Element Id, (double) Starting Opacity, (double) End Opacity, (integer) frames per second, (integer) seconds to run");
return;
}
// The CSS opacity property only works from 1 to 0
if (pStartOpac < 0) {
pStartOpace = 0;
}
if (pStartOpac > 1) {
pStartOpac = 1;
}
if (pEndOpac < 0) {
pEndOpac = 0;
}
if (pEndOpac > 1) {
pEndOpac = 1;
}

// Stop the fps from going over 60 or under 1 (The eye will barely notice
// improvements above 60fps and fractional fps are not supported)
if (fps > 60) {
fps = 60;
}
if (fps < 1) {
fps = 1;
}

var totalFrames = (fps * sec);
var opacityChangePerSecond = (Math.abs(pStartOpac - pEndOpac) / sec);
var opacityChangePerFrame = (opacityChangePerSecond / fps);
var timeOutInterval = 1000 * (1 / fps);

// console.log("totalFrames: "+totalFrames);
// console.log("Opacity change per second: " + opacityChangePerSecond);
// console.log("Opacity change per frame: " + opacityChangePerFrame);
// console.log("Time out interval: " + timeOutInterval + " milliseconds");

var opacity = pStartOpac;
var timeoutVar;
var elemId = document.getElementById(pElem);
elemId.style.opacity = opacity;

if (pStartOpac < pEndOpac) {
fadeIn();
return;
} else {
fadeOut();
return;
}

function fadeIn() {
opacity = opacity + opacityChangePerFrame;
if (opacity > pEndOpac) {
clearTimeout(timeoutVar);
return;
}
elemId.style.opacity = opacity;
timeoutVar = setTimeout(fadeIn, timeOutInterval);
return;
}

function fadeOut() {
if (opacity < pEndOpac) {
clearTimeout(timeoutVar);
return;
}
opacity = opacity - opacityChangePerFrame;
if (opacity < 0) {
opacity = 0;
}
elemId.style.opacity = opacity;
timeoutVar = setTimeout(fadeOut, timeOutInterval);
return;
}

}

最佳答案

遇到问题:当不透明度<0时,将其设置为0,然后执行:if (opacity < pEndOpac) 。 pEndOpac 为 0,因此 0<0 计算结果为 false,并且超时永远不会被清除。解决办法是if (opacity <= pEndOpac) :

function fadeIn() {
opacity = opacity + opacityChangePerFrame;
if (opacity >= pEndOpac) {
clearTimeout(timeoutVar);
return;
}
elemId.style.opacity = opacity;
timeoutVar = setTimeout(fadeIn, timeOutInterval);
return;
}

function fadeOut() {
if (opacity <= pEndOpac) {
clearTimeout(timeoutVar);
return;
}
opacity = opacity - opacityChangePerFrame;
if (opacity < 0) {
opacity = 0;
}
elemId.style.opacity = opacity;
timeoutVar = setTimeout(fadeOut, timeOutInterval);
return;
}

关于javascript - settimeout 调用的函数未结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12244167/

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