Does anyone know what I'm doing wrong?
Basically I'm after loading content (an image) into a div tag, and then refreshing it every x seconds. So this is what I came up with, and it works great until it fades the file back in to which it does a manual refresh.
The current process looks like the following:
Load ... fade out, fade in & then disappears and reappears a few seconds later.
What should be happening is the following:
Load ... fade out, fade in ... fade out, fade in ... (you get the idea, looped)
The code:
$(document).ready(function () {
$("#webcam").load("image.html").fadeIn("slow");
var refreshId = setInterval(function() {
$("#webcam_img").fadeOut("slow");
$("#webcam").load('image.html');
$("#webcam_img").fadeIn("slow");
}, 5000);
});
The online file can be found here: http://colourednoise.co.uk/scripts/webcam.htm
Thank you
load is asynchronous. You are calling fadeIn on an element which is then replaced by the load. You need to fadeIn within the callback of load to ensure that the element exists.
$("#webcam").load('image.html', function(){
$("#webcam_img").fadeIn("slow");
});
Try moving your fadein inside the callback function for the fadeOut so it waits until fadeout is complete first
$("#webcam_img").fadeOut("slow",function(){
$("#webcam").load('image.html');
$("#webcam_img").fadeIn("slow");
});
Here's the full example
$(document).ready(function () {
$("#webcam").load("image.html").fadeIn("slow");
var refreshId = setInterval(function() {
$("#webcam_img").fadeOut("slow",function(){ // <-- added callback function
$("#webcam").load('image.html'); // now this
$("#webcam_img").fadeIn("slow"); // and this waits for fadeOut to complete first
});
}, 5000);
});
Here is a slightly different way to do it, ensuring that each image is fully loaded before the next refresh gets queued:
$(document).ready(function () {
$("#webcam").load("image.html").fadeIn("slow", function () { setTimeout(refreshImage, 5000) });
function refreshImage() {
$("#webcam_img").fadeOut("slow",function(){
$("#webcam").load('image.html');
$("#webcam_img").fadeIn("slow", function() { setTimeout(appendDateToBody, 5000) });
});
}
});
Related
I've been breaking my head over this for several hours now, I'm pretty new to javascript overall so please bear with me.
What I'm trying to achieve:
show a 3 seconds div before a video plays (using the Vimeo api)
What I've tried:
I've tried using .setTimeOut, problem is it only fires once, so the video plays for less than a second and then stops.
.setTimeInterval also doesn't work in my case since it fires at an interval and not continuously.
.delay() is only for jquery effects (correct me if I'm wrong)
What I'm trying now; using a callback parameter
I'm trying to add a callback so it fires my second function (in which the video starts) after it did the first function, with the delay(3000)
My code
// SHOW DIV 3 SECONDS BEFORE VIDEO STARTS
(function($) {
$(document).on("click", "a.short-button", function videoStart(callback) {
$('.short_preloader').fadeIn().delay(3000).fadeOut();
callback();
});
// DO THIS FUNCTION AFTER THE DELAY
videoStart(function() {
var vidframe = $('.embed-container.short-embed iframe'),
status = $('.status');
vidframe(function() {
var player = $f(this);
player.api('play');
});
});
})(jQuery);
My function shows the .short_preloader div for 3 seconds, and should fire the second part of the code afterwards.
Thank you so much in advance!
Victor
fadeOut works asynchronously. You need to wait for it to complete and then call the callback function:
Instead of
$('.short_preloader').fadeIn().delay(3000).fadeOut();
callback();
do this:
$('.short_preloader').fadeIn().delay(3000).fadeOut(callback);
See the following example, which works like you want:
$('button').click(function() {
$('.ad').fadeIn().delay(3000).fadeOut(play);
});
function play() {
$('.video').show();
console.log('playing video');
}
.ad, .video {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Start video</button>
<div class="ad">An ad</div>
<div class="video">Video player</div>
I'm having trouble trying to add a fade effect when I click onto a link. It links to another HTML file that I have for this project. Is it possible to fade in window.location using fadeIn()?
Here's my code:
$("button1").on("click", function() {
window.location.replace("index2.html");
});
$(".button2").on("click", function () {
window.location.replace("index3.html");
});
$(".button3").on("click", function () {
window.location.replace("index1.html");
});
You could try like this:
$("button1").on("click", function() {
$("body").fadeOut(1000,function(){
window.location.href = "index2.html"
})
});
Number 1000 represents the number of milliseconds that you want the fade out effect to last
And, add this to the page you're loading to get the fade in effect when the document is ready (for example in index2.html:
$(function(){
$("body").hide();
$("body").fadeIn(1000);
})
I'm trying to play a sound every time a user gets a new notification. The way I am loading the notifications on my page is simple:
(function($)
{
$(document).ready(function()
{
var $container = $("#noti");
$container.load("notify.php");
var refreshId = setInterval(function()
{
$container.load('notify.php');
}, 1000);
});
})(jQuery);
This works by updating a div container with whatever number the PHP code sends out. it retries every second (probably not the most efficient way, but it works).
I have another piece of code that checks when the div content changes, then creates an alert box (which I will change to playing a sound when the script is done):
var myElement = document.getElementById('noti');
if(window.addEventListener) {
// Normal browsers
myElement.addEventListener('DOMSubtreeModified', contentChanged, false);
} else
if(window.attachEvent) {
// IE
myElement.attachEvent('DOMSubtreeModified', contentChanged);
}
function contentChanged() {
// this function will run each time the content of the DIV changes
alert("js is working");
}
This script works, however it also creates an alert or the first loading of the notifications. This is because it starts of as an empty div, then it loads the data, which sets off this alert script. The only way I could think about going round this is delaying the script from loading for a couple of seconds whilst the AJAX script does its business.
Does anyone know a way I could delay this second script from doing anything for the first few seconds after page load, or perhaps a better way about going round this?
Instead of doing that, use a custom event which you trigger when load finishes:
var refreshId = setInterval(reloadContainer, 1000)
function reloadContainer() {
$container.load('notify.php', function success() {
$container.trigger('loaded')
})
}
$(myElement).on('loaded', contentChanged)
I have a little jQuery function in my Asp.Net MasterPage that fades an image out after 3 seconds. It works fine, but I'm having difficulty getting it to fade back in. I've tried several things as I'm new using jQuery, and I know there's something I'm doing or not doing. I can't put my finger on it. Here's what I have:
<script src="/Scripts/jquery-2.1.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function (){
setTimeout(function (){
$('#Image1').fadeOut('slow', function(){
$('#Image1').remove();
});
}, 3000);
});
var fadeBack = function () {
$('#Image1').fadeIn();
};
fadeBack();
</script>
Like I said, it fades out with no problem, but I cannot find the right code structure to bring it back in. I'm thinking maybe an If statement block about the opacity is needed?
The real trick is that I want alternate images in 3 boxes I have as seen here:
I have about 12 images total, and just want them to fade one image out, and bring another in. Being more specific, I mean the following:
Column (1): Image1.FadeOut(); Image2.FadeIn(); Image2.FadeOut(); Image3.FadeIn(), and etc.
So for now, I just need help with how to do this in Column One, and I'll see if can string something together to make the other Columns 2 and 3 follow up. The timing would be 3 second for each.
Lastly, could I use an array to store other images which aren't in the Column One box already and call them into the slideshow fade sequence? I appreciate you help for this knowledge, so I can lock this in mind. Thanks.
Use this code, it will hide the image after 3 seconds and after that 1 sec, it will show image back.
$(document).ready(function (){
setTimeout(function (){
$('#Image1').fadeOut('slow');
}, 3000);
setTimeout(function (){
$('#Image1').fadeIn('slow');
}, 4000);
});
if you want like a slideshow use this code
<div class="yourimg_container">
<img src="http://localhost/app/img/off.png" id="Image1"/>
</div>
/* make an array containing your images path (you can fetch images from database using asp.net/php query)*/
var ss= ["http://localhost/app/img/off.png",
"http://localhost/app/img/on.png",
"http://localhost/app/img/slider.png"];
window.setInterval(function(){
slideshow();
}, 3000);
function slideshow(){
var im=$("#Image1").attr("src");
for(i=0;i<ss.length;i++){
if(ss[i]==im){
if(i==ss.length-1) $('#Image1').attr("src",ss[0]);
else $('#Image1').attr("src",ss[i+1]);
}
}
}
additionally you can use other effects like this
function slideshow(){
var im=$("#Image1").attr("src");
for(i=0;i<ss.length;i++){
if(ss[i]==im){
if(i==ss.length-1) {
$('#Image1').fadeOut(500);
$('#Image1').attr("src",ss[0]);
$('#Image1').fadeIn(700);
}
else {
$('#Image1').fadeOut(500);
$('#Image1').attr("src",ss[i+1]);
$('#Image1').fadeIn(700);
}
}
}
}
Your fadeBack() is launched immediately whereas the fadeOut has 3 sec delay. Set a timer for your fadeBack grater than 3 sec and the img will appear.
There is a function $('#Image1').remove(); Applied. It mean once fade over, the html block will be removed. Then you can't access the object. Because fade in and fade out accessing same id #Image1. So comment the line. It may work.
So I created some pop-up code that will contain specific information from each link clicked in the pop-up. When closed, the content in the pop-up div gets deleted. Here is my code:
var $content = $('#popupcontent');
var $window = $('#popupwindow');
$('.open').click(function(){
//alert('runnning');
var a = $(this).contents('span');
$content.append(a);
$window.fadeIn(300);
});
$('.close').click(function(){
//alert('running');
var a = $content.contents('span');
$window.fadeOut(300);
$('#popupcontent span').remove();
});
My issue is that it is somehow removing the content before fading out, so the viewer can then see that the pop-up container goes blank. How can I make it so that it will surely fade out first and then remove the content? Here is a Jsfiddle to illustrate that: http://jsfiddle.net/kAdQK/4/
You may want to utilize the complete call back argument for the fadeout method, to remove the element once fadeout is completed. With your current code it will start the fadeout animation and then immediately remove the content without waiting for fadeout animation to complete, hence you get the visual effect that you are seeing now. Using the callback you make sure that it gets executed once the animation is complete.
$window.fadeOut(300, function () {
$('#popupcontent span').remove();
});
Syntax
.fadeOut( [duration ] [, complete ] )
Fiddle
You can use the animation complete to remove your element after the fadeout ends.
The following code will ensure that #popupcontent is removed only after it's faded out
$window.fadeOut('slow', function() {
$('#popupcontent span').remove();
});
Just use a setTimeout();
$window.fadeOut(300);
setTimeout(function(){
$('#popupcontent span').remove();},2000);
Example Here