I've got a simple script that fades in and out some images. It works perfectly fine, before I switch to another tab and then switch back. After switching back seems that interval that runs animation doesn't get cleared and animateFading function keeps running.
Why is it happening, how does tab switching affects script? And how could I make it work properly? thx!
The real code:
var img0 = document.getElementById("image0");
var img1 = document.getElementById("image1");
var img2 = document.getElementById("image2");
var imgs = [img0,img1,img2];
var intervalFading;
var intervalAnimate;
var imgThis = 0;
var imgNext = 1;
var fadeIn = 0;
var fadeOut = 1;
var start;
function animateFading() {
fadeIn+=0.1;
fadeOut-=0.1;
imgs[imgThis].style.opacity = fadeOut;
imgs[imgNext].style.opacity = fadeIn;
if (fadeIn >= 1) {
clearInterval(intervalAnimate);
if (imgNext < 2) {
imgNext++;
} else {
imgNext = 0;
}
if (imgThis < 2) {
imgThis++;
} else {
imgThis = 0;
}
fadeIn = 0;
fadeOut = 1;
}
}
function fading() {
intervalAnimate = setInterval(animateFading,50);
}
function startFading() {
start = setInterval(fading, 3000);
}
window.addEventListener('load', startFading);
Solution:
var startedFading = false;
function startFading() {
if (!startedFading) {
startedFading = true;
startFading = setInterval(fading, 5000);
}
}
function stopFading() {
startedFading = false;
clearInterval(startFading);
}
window.addEventListener('focus', startFading);
window.addEventListener('blur', stopFading);
The boolean is for Chrome, as it fires twice
window.addEventListener('focus', startFading);
&
window.addEventListener('blur', stopFading);
Most browsers have a feature that redirects the memory usage capacity to the only active tab (currently being viewed). Thus, the setInterval and setTimeout timings are affected when you switch the current tab (if you're using Google Chrome, you can see the memory usage of each tab by pressing Shift+Esc. The current tab will use up to 25%, whereas the rest don't pass of the 5%)
Related
I am currently programming a library called quickly.js. There are conceal(milliseconds) and display() functions, which have animations that are programmed through CSS. The conceal function animation is working correctly, but the display function animation does not work correctly. It does not fade in. Instead, it abruptly appears. Here is a JSFiddle demonstrating the bug: https://jsfiddle.net/v6esmqtf/6/.
You simply didn't have the display function set up.
Element.prototype.conceal = function(ms) {
ms = ms || 0;
var thisStyle = this.style;
thisStyle.opacity = 0;
setTimeout(function() {
thisStyle.display = "none";
}, ms);
};
Element.prototype.display = function(ms) {
ms = ms || 0;
var thisStyle = this.style;
thisStyle.display = "";
setTimeout(function() {
thisStyle.opacity = 1;
}, ms);
};
And then...
document.getElementById("conceal").onclick = function() {
document.getElementById("get").conceal(800);
};
document.getElementById("display").onclick = function() {
document.getElementById("get").display(0);
};
Hope this helps.
I am having trouble creating a slider that pauses on hover, because I execute the animation function again on mouse off, if I flick the mouse over it rapidly (thereby calling the function multiple times) it starts to play up, I would like it so that the function is only called if the other function is complete, otherwise it does not call at all (to avoid queue build up and messy animations)
What's the easiest/best way to do this?
$(document).ready(function() {
//get variables
var slide_width = $('.slider_container').width();
var number_of_slides = $('.slider_container .slide').length;
var slider_width = slide_width*number_of_slides;
//set element dimensions
$('.slide').width(slide_width);
$('.slider').width(slider_width);
var n = 1;
$('.slider_container').hover(function() {
//Mouse on
n = 0;
$('.slider').stop(true, false);
}, function() {
//Mouse off
n = 1;
if (fnct == 0) sliderLoop();
});
//Called in Slide Loop
function animateSlider() {
$('.slider').delay(3000).animate({ marginLeft: -(slide_width * i) }, function() {
i++;
sliderLoop();
});
}
var i = 0;
var fnct = 0
//Called in Doc Load
function sliderLoop() {
fnct = 1
if(n == 1) {
if (i < number_of_slides) {
animateSlider();
}
else
{
i = 0;
sliderLoop();
}
}
fnct = 0
}
sliderLoop();
});
The slider works fine normally, but if I quickly move my mouse on and off it, then the slider starts jolting back and forth rapidly...been trying to come up with a solution for this for hours now..
Here's what fixed it, works a charm!
$(document).ready(function() {
//get variables
var slide_width = $('.slider_container').width();
var number_of_slides = $('.slider_container .slide').length;
var slider_width = slide_width*number_of_slides;
//set element dimensions
$('.slide').width(slide_width);
$('.slider').width(slider_width);
var n = 1;
var t = 0;
$('.slider_container').hover(function() {
clearInterval(t);
}, function() {
t = setInterval(sliderLoop,3000);
});
var marginSize = i = 1;
var fnctcmp = 0;
//Called in Doc Load
function sliderLoop() {
if (i < number_of_slides) {
marginSize = -(slide_width * i++);
}
else
{
marginSize = i = 1;
}
$('.slider').animate({ marginLeft: marginSize });
}
t = setInterval(sliderLoop,3000);
});
I'm using this code to make my logo flicker on my website. But It becomes annoying when it continues to flicker while browsing, how can I set a time to allow it to flicker for something like the first 15seconds on page load, then stops?
JS code I'm using:
$(document).ready(
function(){
var t;
const fparam = 100;
const uparam = 100;
window.flickr = function(){
if(Math.round(Math.random())){
$("#logodcoi").css("visibility","hidden");
t = setTimeout('window.unflickr()',uparam);
}
else
t = setTimeout('window.flickr()',fparam);
}
window.unflickr = function(){
if(Math.round(Math.random())){
$("#logodcoi").css("visibility","visible");
t = setTimeout('window.flickr()',fparam);
}
else
t = setTimeout('window.unflickr()',uparam);
}
t = setTimeout('window.flickr()',fparam);
});
You could have a counter, which you then use to decide whether you want to set another timeout. As a side note, you should never add functions to window and then passing a string to setTimeout. Always just pass the function itself:
$(document).ready(function(){
var t;
var amount = 0;
const fparam = 100;
const uparam = 100;
function timeout(f, t) { // this function delegates setTimeout
if(amount++ < 150) { // and checks the amount already (un)flickered
setTimeout(f, t); // (150 * 100 ms = 15 s)
}
}
var flickr = function(){
if(Math.round(Math.random())){
$("#logodcoi").css("visibility","hidden");
t = timeout(unflickr,uparam);
}
else
t = timeout(flickr,fparam);
};
var unflickr = function(){
if(Math.round(Math.random())){
$("#logodcoi").css("visibility","visible");
t = timeout(flickr,fparam);
}
else
t = timeout(unflickr,uparam);
};
t = timeout(flickr,fparam);
});
I see you're using jquery, you could use the following, if I remember correctly, all the stuff I use below has been in jquery since 1.0, so you should be good:
counter = 1;
function hideOrShow(){
$(".classToSelect").animate({"opacity": "toggle"}, 100);
counter = counter +1;
if (counter >= 21) clearInterval(flickerInterval);
}
flickerInterval = setInterval(hideOrShow, 100);
Change the selector, animation duration, and variable names to whatever you fancy/need.
I'm trying to make an object to start/stop blinking (with fadeIn() and fadeOut() for an object in jquery.
I already have a method blink() that makes the element blink once, and it works, but I'm trying to make it blink again as callback to the fadeOut() and don't seem to be able to make it without getting a stack overflow. This is what I've got so far:
Indicator = function(str) {
this.el= $(str);
this.blink = function(){
var callback = function() {
return function(){
console.log(this.el)
this.blink();
}.apply(this);
//if (!this.stopped)
//this.blink();
}.apply(this);
this.el.fadeIn(200).delay(200).fadeOut(200,callback);
}
this.stopped = false;
this.stop = function() { this.stopped = true; }
}
function start(){
indicator =new Indicator('#indicator p');
indicator.blink();
}
(I know my apply()'s are a mess, sorry)
You have created explicit endless recursion. You are calling blink that calls fadeOut that calls callback that calls blink. I would suggest you rewrite this function with setInterval, something like:
this.fadeDuration = 200;
this.blinkIntervalRef = null;
this.blink = function(){
this.blinkIntervalRef = setInterval(
function(){this.doBlink();},
this.fadeDuration*3
);
}
this.stop = function() {clearInterval(this.blinkIntervalRef );}
this.doBlink = function(){
//this is just shortcut, not to make horizontal scroll
var interval = this.blinkIntervalRef;
this.el.fadeIn(interval).delay(interval).fadeOut(interval);
}
Note that it is not tested, but at least it will give you a direction.
Update: here's working example, but requires some debugging and time-spans adjustment:
Indicator = function(str) {
this.el= $(str);
this.fadeDuration = 100;
this.blinkIntervalRef = null;
this.doBlink = function(){
//this is just shortcut, not to make horizontal scroll
var interval = this.blinkIntervalRef;
this.el.fadeIn(interval).delay(interval).fadeOut(interval);
}
this.blink = function(){
var ctx = this;
this.blinkIntervalRef = setInterval(function(){ctx.doBlink();},this.fadeDuration*4);
}
this.stop = function() {clearInterval(this.blinkIntervalRef);}
}
function start(){
indicator = new Indicator('#indicator p');
indicator.blink();
}
$(document).ready(function(){start();});
this is what i once used to do similar kind of thing:
function initializeBlink()
{
$('#indicator p').fadeToggle('slow', fadeToggleBlinker);
}
function fadeToggleBlinker()
{
var timeout = 2000;
if(this.style.display == 'none')
timeout = 1000;
var tmp = $(this);
window.setTimeout(function(){tmp.fadeToggle('slow', fadeToggleBlinker)}, timeout);
}
I was wondering if it's possible to include an swf within a javascript carousel that currently just contains stagic images. What I'm looking to do is include a flash animation within the carousel.
I guess I've got two main questions:
Is it possible to cycle through flash files in the same way as an image?
How would I get the javascript and flash to interact so the flash file would know when it had been selected?
If it helps, here's the js we're using:
$(document).ready(function(){
var $looper = true;
var timer;
var currentSlide = 0;
var cell = 0;
var row = 1;
var hCycles = 0;
var aCycles = 0;
//no. of full cycles
var homecycles = 2;
var aboutcycles = 2;
//aboutSlide speed
var fast = 1200;
var slow = 4000;
//hide homepage slides
$('#slide2').fadeOut(0);
$('#slide3').fadeOut(0);
$('#slide4').fadeOut(0);
$('#slide5').fadeOut(0);
$('#slide6').fadeOut(0);
//hide about slides
$('.a-slide1').fadeOut(0);
$('.a-slide2').fadeOut(0);
$('.a-slide3').fadeOut(0);
$('.a-slide4').fadeOut(0);
$('#slide-c1 .a-slide1').fadeIn(1200);
runSlide(fast);
function runSlide(x) {
if ($('body').is('.about')) {
setTimeout(function() {
aboutSlides();
}, x);
} else {
if ($looper) {
setTimeout(function() {
slideShow();
}, 4000);
}
}
}
function slideShow() {
if ($looper) {
if (currentSlide++ < 6 && hCycles < homecycles) {
$('#slide'+ currentSlide).fadeOut(1200);
if (currentSlide == 6) {
$('#slide1').fadeIn(1200);
$('#slide-wrapper li').removeClass('active');
$('#btn1').addClass('active');
currentSlide = 0;
hCycles = hCycles+1;
} else {
$('#slide'+ (currentSlide+1)).fadeIn(1200);
$('#slide-wrapper li').removeClass('active');
$('#btn'+ (currentSlide+1)).addClass('active');
}
runSlide();
} else {
$looper = false;
}
}
};
$('#slide-wrapper li').each(function(index) {
$('#btn'+(index+1)).click(function(){
$looper = false;
$('.slide').fadeOut(1200);
$('#slide'+ (index+1)).fadeIn(1200);
$('#slide-wrapper li').removeClass('active');
$(this).addClass('active');
});
});
function aboutSlides() {
if (cell++ < 3 && aCycles < aboutcycles) {
if (cell == 3) {
if (row < 3) {
row = row+1;
} else {
row = 1;
aCycles = aCycles+1;
}
var hide = (row-1);
if ((row-1) == 0) {hide = 3}
$('#slide-c1 .a-slide'+ hide).fadeOut(1200);
$('#slide-c1 .a-slide'+row).fadeIn(1200);
cell = 0;
runSlide(fast);
} else {
$('#slide-c'+(cell+1)+' .a-slide'+ (row-1)).fadeOut(1200);
$('#slide-c'+(cell+1)+' .a-slide'+(row)).fadeIn(1200);
if (cell == 2) {
runSlide(slow);
} else {
runSlide(fast);
}
}
} else {
// create the final strip
$('#slide-c3 .a-slide3').fadeOut(1200);
$('#slide-c3 .a-slide4').fadeIn(1200);
}
}
});
Thanks!
There isn't any problem as to whatever content you want to put in your slides. As long as it is valid html, it's valid in a slide. Countless jquery/motools/etc plugins let you specify whatever you want for content.
flash is valid.
But you might want to revert to another revealing method. setting the opacity on a swf from javascript is complex and yields to different results, according to browser and flash version. If your flash file is custom made, then you can create a function that fades it to white for example, and call it from javascript. But from experience, changing the opacity of a swf is calling for trouble.
I don't know if this is relevant enough to be an answer, I wanted to post it as a comment, but there isn't any comment button. Oh well.