Javascript setInterval函数在切换到其他窗口时试图赶上
我有一个代码,循环通过2个图像 淡出第一个图像,更改图像,然后淡入第二个图像。 我用setInterval函数重复这个过程 当我看循环时,它看起来很好。 当我切换到另一个选项卡并在10秒或20秒后返回时,就像试图快速赶上我错过的所有淡入淡出和更改。。。(简而言之,快疯了) 下面是setInterval函数:Javascript setInterval函数在切换到其他窗口时试图赶上,javascript,Javascript,我有一个代码,循环通过2个图像 淡出第一个图像,更改图像,然后淡入第二个图像。 我用setInterval函数重复这个过程 当我看循环时,它看起来很好。 当我切换到另一个选项卡并在10秒或20秒后返回时,就像试图快速赶上我错过的所有淡入淡出和更改。。。(简而言之,快疯了) 下面是setInterval函数: setInterval(function(){ fadeout(PointerToImage, function() { ChangeImage(function() {fadein(Poi
setInterval(function(){
fadeout(PointerToImage, function() {
ChangeImage(function() {fadein(PointerToImage); } )
});
},5000);
如有需要:
淡出功能:
function fadeout(element,complete) {
var op = 1; // initial opacity
var timer = setInterval(function () { //call the function every 50 milliseconds
if (op <= 0.1){ // stop the function when the op is lower then x and then make in invisible.
clearInterval(timer); // clear the timer ....
if ( typeof complete === 'function' ) // run it only if complete is a function .....
{
complete();
}
return true;
}
element.style.opacity = op; // for newer browsers
op -= op * 0.01; // reduce 10% each time
}, 0.05); // run every 50 seconds
}
function fadein (element,complete) {
var op = 0.1 // initial opacity
var timer = setInterval(function () { //call the function every x milliseconds
if (op >= 0.99){ // stop the function when the op is higher then x ....
element.style.opacity = 1;
clearInterval(timer); // clear the timer ....
if ( typeof complete === 'function' ) // run it only if complete is a function .....
{
complete();
}
return true;
}
element.style.opacity = op; // for newer browsers
op += 0.01; // reduce 10%
}, 30); // run every 50 seconds
}
function ChangeImage (complete) {
liran0.src=imagesarrays[indexid1];
indexid1=indexid1 +1;
if(indexid1 >= imagesarrays.length){
indexid1 = 0;
}
更改图像功能:
function fadeout(element,complete) {
var op = 1; // initial opacity
var timer = setInterval(function () { //call the function every 50 milliseconds
if (op <= 0.1){ // stop the function when the op is lower then x and then make in invisible.
clearInterval(timer); // clear the timer ....
if ( typeof complete === 'function' ) // run it only if complete is a function .....
{
complete();
}
return true;
}
element.style.opacity = op; // for newer browsers
op -= op * 0.01; // reduce 10% each time
}, 0.05); // run every 50 seconds
}
function fadein (element,complete) {
var op = 0.1 // initial opacity
var timer = setInterval(function () { //call the function every x milliseconds
if (op >= 0.99){ // stop the function when the op is higher then x ....
element.style.opacity = 1;
clearInterval(timer); // clear the timer ....
if ( typeof complete === 'function' ) // run it only if complete is a function .....
{
complete();
}
return true;
}
element.style.opacity = op; // for newer browsers
op += 0.01; // reduce 10%
}, 30); // run every 50 seconds
}
function ChangeImage (complete) {
liran0.src=imagesarrays[indexid1];
indexid1=indexid1 +1;
if(indexid1 >= imagesarrays.length){
indexid1 = 0;
}
浏览器可能会减慢当前未显示的浏览器窗口中的计时器。这既可以延长电池寿命(在电池供电设备上),也可以提高所显示窗口的性能。