Second div to autoplayed always 1 sec after the first div - javascript

My question is how can I have #slider to be played always 1 second after #faded. Having it in 4 secs and 5 secs is not a stable way.
$(function(){
$("#faded").faded({
autoplay: 4000
});
});
$(function(){
$("#slider").faded({
autoplay: 5000
});
});

Is this what you want / need? It will fade in the first element after 4 seconds and when that is done, it will trigger a second timer after 1 second:
setTimeout(function() {
$('#faded').fadeIn('normal', function() {
setTimeout(function() {
$('#slider').fadeIn('normal');
}, 1000);
});
}, 4000);

Here is one problem you are facing:
$(function(){... this event happens before the page is render, thus creating inconsistencies in your code.
I would recommend that you change it to window.onload = function (){....
this should fix the problem.

Related

How do I do 3 animations in jQuery and loop them? (Timing is messed up.)

I have a three different transitions for fading out and fading in my 3 images.
animate1 = function() {
$('#changingImage').fadeOut('fast', function() {
$('#changingImage').attr("src","../files/others/image1.jpg");
$('#changingImage').fadeIn('fast');
});
};
I have this same function two more times, just replacing the 1s with 2s and 3s.
I call my three functions with this, repeating every 5 seconds:
animate1().delay(5000);
animate2().delay(10000);
animate3().delay(15000);
I'm new at jQuery, and I don't understand why the timing is wrong. All that happens is image2 (the original) gets replaced with image1, and that's it.
Try using the setTimeout() javascript function.
Documentation: http://www.w3schools.com/jsref/met_win_settimeout.asp
For example:
setTimeout(function(){ animate1(); }, 5000);
setTimeout(function(){ animate2(); }, 5000);
setTimeout(function(){ animate3(); }, 5000);
This basically 'pauses' your JavaScript/jQuery code for 5 seconds before running the function and continuing.
.delay() does not repeat an event, it just delays its execution. You need .setInterval() if you want to repeat an event based on a given interval:
window.setInterval(function(){
setTimeout(animate1, 1000);
setTimeout(animate2, 500);
}, 5000);
Demo: https://jsfiddle.net/erkaner/bfb7jgaL/1/
Yay! I figured it out with a bunch of help.
var animations = function() {
setTimeout(function() {
animate1();
console.log("Animation 1")
}, 5000);
setTimeout(function() {
animate2();
console.log("Animation 2")
}, 10000);
setTimeout(function() {
animate3();
console.log("Animation 3")
}, 15000);
};
setInterval(animations, 15000);
Hi i have a suggestion for you in case that you have more than this 3 image so you will create a new function for it ?
so what about to use only 1 function that will call it self with an attribute that define image name as it is the only thing is changing every time so you can use this better and less code ,you have just the n value will change every time will increase and the max value witch define how many image you want
//if you want to set this animation for more than 3 image just change the max value
var max=3;
setTimeout(function(){ animate(2); }, 5000);
animate = function(n) {
$('#changingImage').fadeOut('fast', function() {
if(n<=max){
$('#changingImage').attr("src","http://www.imacros-scripts-for-free.is-best.net/image"+n+".jpg");
$('#changingImage').fadeIn('fast');
if(n==max){n=1}else{n++;}
setTimeout(function(){ animate(n); }, 5000);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="changingImage" src="http://www.imacros-scripts-for-free.is-best.net/image1.jpg" width="200px">

fadeOut work immediately

I have some div in my page that when an user drags one of them the div would fade out.It's work but div fadeout after 6 second immediately.
$(function(){
$( ".comment-list.clearfix" ).draggable({axis: "x"},{
start: function() {
$(this).fadeOut(6000);
},
});
});
Use setTimeout() for delay like,
start: function() {
var $this=$(this);
setTimeout(function(){
$this.fadeOut(6000);
},5000); // 5 seconds timeout, for example
}
or use delay() like,
start: function() {
$(this).delay(5000) // 5 seconds delay, for example
.fadeOut(6000);
}
Note: You can change delay interval, as you required.

show div after 5 seconds and then refresh the div every 1 min

I have created a div into my page which i wanted to get load after 5 seconds the page get load. Then I wanted to refresh the div without refreshing the page after every 1 min. How can I achieve this functionality with the use of J query. My code look like this
$(document).ready(
function () {
setInterval(function() {
$('#newsletter').show();
}, 100000);
});
This upper block of code is only refreshing the div after 1 min. But on page load , i want the div to be shown to the user after 5 seconds and then this block of code should executed.
$(document).ready(function()
{
setTimeout(function()
{
// Perform actions you need to
setInterval(function()
{
// Perform them again every minute
}, 60000);
}, 5000);
});
I think what you were looking for is setTimeout
Try it:
$(function(){
setTimeout(function(){
$('#newsletter').show(); // to show div after 5 sec of page load
// To reshow on every one minute
setInterval(function() {
$('#newsletter').show();
}, 60000);
}, 5000);
});
You've already got the base functionality there, so all you'll need to do is add a message to appear after 5 seconds. Something like this should work.
$(document).ready(function()
{
setTimeout(function() {
// show message
},5000);
setInterval(function() {
$('#newsletter').show();
}, 60000);
});
Here is a jQuery solution:
$(document).ready(function () {
var $newsletter = $('#newsletter')
.delay(5000)
.show(function() {
setInterval(function() {
// update the newsletter, e.g. set new text
$newsletter.text(new Date());
}, 60000);
});
});

add autoplay to basic jquery slider

Im making a basic jQuery slider for class. and would like to have it autoplay playing the next slide after 5 seconds but haven't used the timer before it isn't doing what I thought it should do. It waits for the proper interval and then keeps triggering the callback function.
//basic slider on click
$('.slider>ul>li').click(function()
{
$('.slider>ul>li').removeClass('current');
$(this).toggleClass('current');
right=$(this).index()*745;
$(this).parent('ul').parent('.slider').children('div').children('ul').animate({'right': right}, 1000, 'easeOutBack');
})
//attempt to use timer plugin to auto play
$('.slider>ul>li').timer(
{
delay: 1000,
callback: function()
{alert($('.slider>ul>li.current').index())
if($('.slider>ul>li.current').index()!=3)
{
$('.slider>ul>li.current').next('li').click();
}else//oddly this doesn't trigger at all
{
$('.slider>ul').first().click();
}
}
})
Try this:
setInterval(function(){
var i = $('.slider>ul>li.current').index();
var next = (i < 3 ? i+1 : 0);
var items = $('.slider>ul>li');
$(items[next]).click();
}, 5000);
EDIT:
changed setTimeout to setInterval and bumped time to 5sec; this should now loop.

How do I trigger a function after the user has had his mouse over an element for x seconds?

Here's my code:
$('#element').hover(function()
{
window.setTimeout(function()
{
alert('hovered for 2 seconds');
}, 2000);
},
function()
{
alert('mouse left');
});
But it doesn't quite work the way I want it to.
What I'm actually trying to do is, if the user has his mouse over the #element for 2 seconds it should trigger the alert(). But if he moves his mouse away from the #element before the 2 seconds are over nothing should happen.
And the problem with the current code is that if I move my mouse over the #element and move it away before the 2 secs are over, it still triggers the alert() after 2 secs.
I really hope this makes sense.
You are almost there. You need to store the result of setTimeout (I used jQuery's data), then call clearTimeout with the same value.
$('#element').hover(function()
{
$(this).data('timeout', window.setTimeout(function()
{
alert('hovered for 2 seconds');
}, 2000));
},
function()
{
clearTimeout($(this).data('timeout'));
alert('mouse left');
});
http://jsfiddle.net/nCcxt/

Categories