How do I execute the flipclock once the fadeIn animation is finished?
I want to start and show, maybe even fade in a clock once my fadeIn() animation for the video has completed. Currently it fades the video in, but when I added queue() which I read this was the recommended option to achieve this my code broke.
I think my overall syntax is wrong, that's why my error occurs? (or maybe I'm just going about this completely wrong.
This is my code:
var video= document.getElementById('vid');
var overlay= document.getElementById('overlay');
var timer;
var clock;
// Hides recorded video until timer reaches 0
// TODO: Should start recording once video appears fully on screen not while being hidden.
$("#recording").hide();
// Counter;
function countdown(seconds, callback){
timer = setInterval(function(){
document.getElementById("coutdownText").innerHTML = "Recording will start automatically in: " + seconds;
if(seconds == 0){
$("#vid").show();
setTimeout(function() {$("#vid").fadeOut(2500);}, 0)
overlay.style.visibility ='hidden';
// console.log("Worked the seconds is:" + seconds)
setTimeout(function() {$("#recording").fadeIn(2500).queue(function(next){
// FlipClock : My problem occurs when I try to use queue
$(document).ready(function() {
// Instantiate a counter
clock = new FlipClock($('.clock'), 600, {
clockFace: 'MinuteCounter',
autoStart: true,
countdown: true
});
});
next();
}));}, 3000)
}
seconds-- || (clearInterval(timer), callback());
}, 1000);
}
overlay.style.visibility = "hidden";
// Check for when PreRecorded video ends.
video.addEventListener('timeupdate', function() {
if(video.ended){
overlay.style.visibility ='visible';
countdown(10, function(){ console.log("Coutdown done!") });
}
})
Related
This animation should stop playing (stop snowing) after 10 seconds. I added a function to the bottom, but it actually starts the animation after 10 seconds, rather than stopping the animation after 10 seconds.
I only want it to play for 10 seconds total time, and start right-away, see my codePen to see what I mean.
setTimeout(function() {
createSnow(20);
loop();
}, 10000)
here is a codePen to see what I mean:
https://codepen.io/celli/pen/XzzjRW
You were close. You just needed a way to tell the system to stop the animation loop. See my codepen: https://codepen.io/intervalia/pen/zPPoBZ
I added a variable to indicate if animation should be happening:
var animating = true;
A routine to stop the animation:
function stopAnimation() {
animating = false;
//Add something in here to clear the screen
}
A validation check in the animation loop:
function loop() {
if (animating) {
draw();
update();
animFrame(loop);
}
}
And changed your timeout to turn off the animation:
createSnow(150);
loop();
setTimeout(stopAnimation, 10000);
Cool animation!!
What you'll want to do is create a "timer", finding the difference between when you started and the current time.
I'm not sure if you wanted createSnow(20) inside of the loop or not, so I put it before, as initialization.
createSnow(20);
const start = new Date();
while(new Date() - start < 10000) {
loop();
}
Use cancelAnimationFrame.
Sample usage
Declare cancelAnimFrame:
var cancelAnimFrame =
window.cancelAnimationFrame ||
window.mozcancelAnimationFrame ||
window.webkitcancelAnimationFrame ||
window.mscancelAnimationFrame;
Update loop function:
var animId;
function loop() {
draw();
update();
animId = animFrame(loop);
}
and use animId to stop animation.
(As already mentioned, starting snowing should be out of setTimeout.)
createSnow(20);
loop();
setTimeout(function() {
cancelAnimFrame(animId)
}, 1000)
the only thing that you have to change is a little change of your code plz see this code below :
var timeOut = setTimeout(function() {
createSnow(20);
loop();
}, 10000);
clearTimeout(timeOut);
I am working on a pure JavaScript game I have a plane that shoots missiles so the idea is when i click it shoots a missile and after a few seconds the missile is back to its position and displayed again its working fine but when i click multiple times it is stack so what is happening is there are many clicks in which the missile is not back to it's position how can i solve this ?? how can i allow only one click for example in a period on 3 seconds ? or allow clicking only when the missile is ready !!!
here is my code !
window.onclick = function()
{
var $ball1 = document.getElementById("ball1");
// shooting the missile using css transition to get some delay
$ball1.style.top = "-12000px";
// hide missile and get it back to it's position
setTimeout(function(){
$ball1.style = "display:none; top:71px";
}, 500);
// show missile again on plane
setTimeout(function(){
$ball1.style = "display:block;";
}, 1000);
}
A simple way would be to use a variable to store the last time when a click was handled, then check for the time that has passed. In my example, I use lastTime to store the time and I implement a gap of 3000ms (3 seconds) between clicks. The output of this example is simple logging to the console, but you can change it to whatever you wish.
var lastTime = -1;
window.onclick = function() {
if (lastTime < 0 || (new Date().getTime() - lastTime >= 3000)) {
lastTime = new Date().getTime();
console.log("firing missile");
} else {
console.log("too soon");
}
}
To solve the issue you're facing you need to store a state allowNextClick, based on which you'll decide whether to execute the further code or not.
var allowNextClick = true;
window.onclick = function()
{
if(!allowNextClick) {
return;
}
allowNextClick = false;
// allow every 3 seconds
setTimeout(function() {
allowNextClick = true;
}, 3000);
var $ball1 = document.getElementById("ball1");
// shooting the missile using css transition to get some delay
$ball1.style.top = "-12000px";
// hide missile and get it back to it's position
setTimeout(function(){
$ball1.style = "display:none; top:71px";
}, 500);
// show missile again on plane
setTimeout(function(){
$ball1.style = "display:block;";
// allow next click after missile is back
allowNextClick = true;
}, 1000);
}
// define a Boolean to check if ball is just shoot
var canShot = true;
window.onclick = function() {
if (canShoot) {
var $ball1 = document.getElementById("ball1");
// shooting the missile using css transition to get some delay
$ball1.style.top = "-12000px";
// turn the Boolean canShot to false to prevent multiple trigger
canShot = false;
// hide missile and get it back to it's position
setTimeout(function(){
$ball1.style = "display:none; top:71px";
}, 500);
// show missile again on plane
setTimeout(function(){
$ball1.style = "display:block;";
// turn the Boolean canShot to true to make the ball can be shoot
canShot = true;
}, 1000);
}
}
I have made a custom slider with jQuery. For this I have used setInterval function:
timer = setInterval(function() {}, 8000);
But I cannot pause and resume the interval. I have 2 buttons (play, pause) which I want to use for. Lets say I click pause after 3 sec, and then resume it. So it should stay in that slider for 5 more seconds and then go to the next one and continue 8 seconds each. I have seen this kinda slider with mouseover pause, but can't do it by myself. I have tried this:
clearInterval(timer);
But this seems reset the interval, don't pause. Can anyone help :)
I'm not entirely sure that's something native to jQuery, however, you could use a flag to pause it, and check in your setInterval whether to execute.
Edit:
Found something that might be useful to you, the jquery-timer
Alternitively, you can keep track of the id set by setInterval, and clear out out when you'd like to pause. Then you can set it again when you wish to resume:
var id = window.setInterval(<code>); //create
window.clearInterval(id); //pause
id = window.setInterval(<code>); //resume
there are two ways of accomplish this:
Clearing the interval everytime you pause and starting a new interval when you resume it.
Having a flag to tell the function in the interval when it is paused and it should not do anything.
The first solution would work like this:
let intervalId = false;
const intervalLength = 8000; // 8 seconds
function intervalFunction () {
// do stuff.
}
startButton.onclick = function () {
if (intervalId === false) {
intervalId = setInterval(intervalFunction, intervalLength);
}
}
pauseButton.onclick = function () {
if (intervalId !== false) {
clearInterval(intervalId);
intervalId = false;
}
}
// auto start it:
intervalId = setInterval(intervalFunction, intervalLength);
The second solution would work like this:
var isRunning = true;
var interval = setInterval(function() {
if (!isRunning) {
// not running, do nothing
} else {
// it is running, do stuff.
}
}, 8000);
pauseButton.onclick = function () {
isRunning = false;
};
startButton.onclick = function () {
isRunning = true;
};
I am not complete sure, that what you are asking for, is the right thing you are showing us... setInterval basically is pure native javascript and in my opinion not worth using! If you wan't to set your timeouts via jquery try this link: http://jchavannes.com/jquery-timer. You can find usages there...
Now on to your problem... you need a state to check wether the slider has to slide or not... simply set a bool like this...
var timer;
var doSlide = false;
var i = 0;
function Slide(){
timer = setTimeout(function(){
if(doSlide == true){
Slide();
i++; // Same as i = i + 1
console.log('Sliding');
if(i == 3) AbortSlide(); /* Abort on third slide! Dont use this in your logic!*/
} else if(doSlide == false){
console.log('Sliding aborted untill next RunSlide() call!')
clearTimeout(timer);
}
},1000);
}
function AbortSlide(){
doSlide = false;
i = 0; // Resetting the count! Dont use this in your logic!
}
function RunSlide(){
doSlide = true;
Slide();
}
RunSlide();
You could also empty the interval in the abort method:
function AbortSlide(){
doSlide = false;
clearTimeout(timer);
i = 0; // Resetting the count! Dont use this in your logic!
}
Here is a working fiddle i made for you to understand what timers and intervals are for: https://jsfiddle.net/q5qzmv68/7/
Hope this helps! Cheers!
so i have this loading screen that displays information while the page is loading but because of faster internet speeds this can be displayed in a flash.. I would like to add a minimum display time! this is my current code
jQuery(window).load(function() {
jQuery('.pageLoad').animate({
opacity: 0
}, 800, function() {
jQuery('.pageLoad').css({
display: 'none'
});
});
});
How can i do this?
You could put your fade-out method in a function and call that after an xx number of seconds from $(document).ready():
var timeoutID;
jQuery(document).ready(function() {
// start hiding the message after 2 seconds
timeoutID = window.setTimeout(hideMessage, 2000);
});
function hideMessage() {
jQuery('.pageLoad').animate({
opacity: 0
}, 800, function() {
jQuery('.pageLoad').css({
display: 'none'
});
});
}
As mentioned by #Rayf, if that piece of info should be read, regardless of page loading speed, you should add some delay to it like this:
// time to read the message...adjust it to the time you feel is right
var msgDisplayTime = 5000,
interval = 0,
loaded = false,
delayed = false,
fadeLoader = function () {
jQuery('.pageLoad').animate({opacity: 0}, 800, function () {
jQuery('.pageLoad').css({display: 'none'});
});
};
//timeout for your desired delay time
//it will ask if it is already loaded after delay ends
//triggering the fading of loading overlay
timeout = setTimeout(function(){
delayed = true;
if(loaded){
fadeLoader();
}
},msgDisplayTime);
//when loaded, it will wait until delay happened
//if not, it will delegate the execution to the timeout
// so at the end, it will be triggered after the delay or
//loading time, in case it longer than desired delay
jQuery(window).load(function(){
loaded = true;
if(delayed){
fadeLoader();
}
});
Inside comments is the roughly explanation about how it works
I have a simple function that I wrote that transitions three div elements using a fade in/out effect. The event is triggered when a user clicks a link. Here's my code:
$(".link1").click(function () {
$(".feature1").fadeIn(1000);
$(".feature2").fadeOut(1000);
$(".feature3").fadeOut(1000);
});
$(".link2").click(function () {
$(".feature1").fadeOut(1000);
$(".feature2").fadeIn(1000);
$(".feature3").fadeOut(1000);
});
$(".link3").click(function () {
$(".feature1").fadeOut(1000);
$(".feature2").fadeOut(1000);
$(".feature3").fadeIn(1000);
});
I need to be able to set some sort of timer so that these transitions happen automatically every 8 seconds or so. I also want them to "loop" essentially, so that if we get to the third div in the set, it returns to the first div.
Try using setTimeout() function.
var timer = null;
function foo_loop(div, timeout) {
if (div > 3) div = 1;
$(".feature"+div).fadeIn(1000);
$("div[class^=feature]:not(.feature"+div+")").fadeOut(1000);
clearTimeout(timer);
timer = setTimeout(function() {
foo_loop(div + 1, timeout);
}, timeout);
}
Run this like that (To start with first div and 8 second timeout):
foo_loop(1, 8000);
Function for stopping loop:
function stop_loop() {
clearTimeout(timer);
}
Run it when you need to stop the loop (for example on click on element with id="stop"):
$('#stop').bind('click', stop_loop);
setInterval(expression, timeout); runs the function in intervals, with the length of the timeout between them
example:
var intervalID = setInterval(alert('heelo'), 3000); // will alert hello every 3 seconds
// clearInterval(intervalID); // will clear the timer
Try the following:
var i = 0;
var transition = setInterval(function(){
i++;
if (i == 4) {i = 1}
$(".feature"+i).stop().fadeIn(1000, function(){
$(this).delay('6000').fadeOut(1000)
})
}, 8000)
not sure I fully understand when you want them to loop or how often, but I think this should help you out a bit... every 8 seconds it loops through the animations...
function fadeLoop(selectors, animations, times, index) {
index = index || 0;
if(index == selectors.length) return;
$((selectors[index])[animations[index]](times[index], function() {
fadeLoop(selectors, animations, times, index + 1);
});
}
setInterval(function() {
fadeLoop(
['.feature1', '.feature2', '.feature3'],
['fadeIn', 'fadeOut', 'fadeOut'],
[1000, 1000, 1000]
);
fadeLoop(
['.feature1', '.feature2', '.feature3'],
['fadeOut', 'fadeIn', 'fadeOut'],
[1000, 1000, 1000]
);
fadeLoop(
['.feature1', '.feature2', '.feature3'],
['fadeOut', 'fadeOut', 'fadeIn'],
[1000, 1000, 1000]
);
}, 1000 * 8);