Jquery fadeIn in callback for fadeOut not lasting - javascript

Hope someone can shed some light on this issue for me.... I'm using a setInterval to alternate displaying headlines on a webpage. it fades out the previous one, then in the callback function fades in the new one. it used to work fine, however I separated the callback function from the fadeOut because I wanted to run it initially without a delay, and now I'm getting the initial headline, however when it comes time to change, they fade in for a split second and disappear again.
function processSidebar(data) {
var headlines = $.parseJSON(data);
var sIndex = 0;
function newSidebar(surl, sIndex) {
$(".sidebar").html(headlines[sIndex].date + '<br>' + headlines[sIndex].line + '');
$(".sidebar").fadeIn(400);
}
newSidebar("getme.php?blog=1&headline=1", sIndex);
setInterval(function() {
++sIndex;
if (sIndex == headlines.length) {sIndex = 0}
var surl="getme.php?blog=1&headline=" + headlines[sIndex].index;
$(".sidebar").fadeOut(400,newSidebar(surl,sIndex));
}, 10000); // end setInterval
}; // end processSidebar

jQuery's fadeOut wants a function as the complete argument.
You're giving newSidebar(surl,sIndex), which gets evaluated immediately and returns nothing (but does the whole fadeIn stuff).
You want to use an anonymous function:
$(".sidebar").fadeOut(400,function() { newSidebar(surl,sIndex) });

Related

Jquery, line in callback function doesnt wait execution

I want to create a nice animation where a paragraph fade, change the content, unfade, change again and so on. the thing is the line where I set the paragraph content got executed before the fade-unfading things finished.
function displayAnim(x){
let textBox = document.getElementById("resultBoxText");
fadeO(textBox,x,0);
}
function fadeO(elem,x,i){
$("#resultBoxText").fadeIn("2000",next(elem,x,i));
}
function next(elem,x,i){
elem.innerHTML = result[x][i]; //this line got executed well, but it executed 5 times already even before the first unfade happens
let nextI = i+1;
if(nextI >= 6){
$("#resultBoxText").fadeOut("2000",show(x));
return;
}else{
$("#resultBoxText").fadeOut("2000",fadeO(elem,x,nextI));
}
}
function show(x){
let textBox = document.getElementById("resultBoxText");
textBox.innerHTML = result[x][6] //it reach this line already. this line is supossed to be executed after the fades occur 5 times
}
what am I doing wrong?
thanks (^_^)

Trying to make the clock function on click

And thanks for stopping by.
I've been trying to add a feature, or function, to a project that I'm working on, but I just can't wrap my head around it.
When my project loads I want the user to activate the time as soon as they move, or click, one of the orbs -- not as soon as the project loads. I know there's an onClick or click event, but I'm having trouble implementing it on the js code. I've gone as far as commenting-out or "greying" entire functions to test things out.
Here's some of the time code:
function clockStart() {
numberOfSecs = setInterval(function () {
seconds.text(`${second}`)
second = second + 1
}, 1000);
}
function rewindSec(seconds) {
if (seconds) {
clearInterval(seconds);
}
}
gameStart();
// GAMES BEGIN!!!
function gameStart() {
var cards = shuffle(listOfCards);
deckOfCards.empty();
match = 0;
Moves = 0;
moveNum.text("0");
ratingStars.removeClass("fa-angry").addClass("fa-star");
for (var i = 0; i < cards.length; i++) {
deckOfCards.append($('<li class="card"><i class="fab fa-' + cards[i] + '"></i></li>'))
}
sourceCard();
rewindSec(numberOfSecs);
second = 0;
seconds.text(`${second}`)
clockStart();
};
Here's a link to the project that I'm talking about:
Full Page View: https://codepen.io/idSolomon/full/RYPZNp/
Editor View: https://codepen.io/idSolomon/pen/RYPZNp/?editors=0010
WARNING: Project not mobile-friendly. At least not yet.
If someone can help me out with this, a thousand thank yous will come your way.
Thanks!
You can Change the event of starting the clock at another position
First remove the clockStart() function from the gameStart() method
seconds.text(`${second}`)
//clockStart();
if have added an Extra variable to detect if the clock is started or not
var isClockStarted=false;
The following code will start the timer when a card is clicked:
var sourceCard = function () {
deckOfCards.find(".card").bind("click", function () {
if(!isClockStarted)
clockStart();
The Bellow Code will check if the timer is started or not.
function clockStart() {
if(!isClockStarted){
isClockStarted=true;
numberOfSecs = setInterval(function () {
seconds.text(`${second}`)
second = second + 1
}, 1000);
}
}
I have found that you are not stopping the timer even after the game finishes
If canceled a game it starts timer again //toCheck

jquery .fadein .fadeout callback nesting not ordering correctly

I am trying to nest fadein and fadeout methods in order to get the following effect. I would like the characters in 10 seperate tags to one by one fadeout, change text and then fade in. With what I know about callback functions the following code should achieve that but the effect I am getting now is all the elements change simultaneously and the text change does not wait for the fadeout resulting in the text flashing and then fading out and in. What am I doing wrong!! Here is a simplified version of what I have currently.
HTML/PHP:
<?php
echo '<div onclick="addname(this)">';
for ($i=0; i<10; i++){
echo '<h1 id="temp'.$i.'">Y</h1>';
}
?>
Javascript
function addname(item){
//Other stuff that works fine
hideChar(0);
}
function hideChar(i){
if(i<10){
var tag = "temp" + i;
var x = document.getElementById(tag);
$(x).fadeOut(200, showChar(i));
}
}
function showChar(i){
var tag = "temp" + i;
var x = document.getElementById(tag);
var j = i+1;
$(x).html("X");
$(x).fadeIn(200, hideChar(j));
}
You're not passing your function as the callback properly. When you include the () - the function will be triggered immediately - omit the () to pass it along:
$(x).fadeOut(200, showChar);
The default parameters will be passed along. If you need something other than the default - you'll have to use an anonymous function to call yours:
$(x).fadeOut(200, function() {
showChar(i)
});

Why is this jquery .each loop stopping my function call in the middle?

This is the entire function I am working on:
function collapse(){
var i = 0;
var currColors = [];
var currTitles = [];
// Get the color and the course id of all the current courses
$('.course').each(function (){
if (rgb2hex($(this).css("background-color")) !== "#f9f9f9") {
currColors.push(rgb2hex($(this).css("background-color")));
currTitles.push($(this).children(".course-id").text());
$(this).children(".course-delete").remove();
$(this).children(".course-id").text("");
$(this).css('background', "#f9f9f9");
}
});
alert("made it");
// Redistribute the classes
// This is where reverse lookup will eventually happen
i = 0;
$('div.course').each(function (){
if (i>=currTitles.length){
return false;
}
$(this).children(".course-id").text(currTitles[i]);
$(this).css('background', currColors[i++]);
$(this).append("<button id='temp' class='course-delete'>X</button>");
});
var i = currColors.length-1;
};
And this is the problematic section
$('.course').each(function (){
if (rgb2hex($(this).css("background-color")) !== "#f9f9f9") {
currColors.push(rgb2hex($(this).css("background-color")));
currTitles.push($(this).children(".course-id").text());
$(this).children(".course-delete").remove();
$(this).children(".course-id").text("");
$(this).css('background', "#f9f9f9");
}
});
I have this function collapse that is supposed to fill in the blank spot in a list of divs that the user has been adding to the screen if they delete one. This function was working fine at one point, but obviously I have screwed it up some how.
There are only and always will be 6 '.course' items because that is the size of the list. When collapse() is called it stops running after n times where n is the number of '.course' elements currently in use. The loop stops there and the whole function stops there. I have tried putting alert() statements everywhere but still no luck. If someone spots what I am missing I would really appreciate it.
I have figured out a work around for now, but I would like to know why my entire function is crashing in the middle.

Help with JQuery Callback

As per my previous question, I have a working animation which fades in and out each element within the div slideshow. The problem is that I want this animation to continue from the beginning once it has reached the last element. I figured that was easy and that I'd just place an infinite loop inside my JQuery function, but for some reason if I insert an infinite loop, no animation displays and the page hangs. I also cannot find anything in the documentation about how properly place a callback. How can I get this code to restart from the beginning of the animation once it finishes iterating over each object and why is an infinite loop not the right way to go about this?
<div id="slideshow">
<p>Text1</p>
<p>Text2</p>
<p>Test3</p>
<p>Text4</p>
</div>
<script>
$(document).ready(function() {
var delay = 0;
$('#slideshow p').each(
function (index, item)
{
$(this).delay(delay).fadeIn('slow').delay(800).fadeOut('slow');
delay += 2200;
}
);
});
</script>
You could do something like this:
$(function() {
var d = 2200;
function loopMe() {
var c = $('#slideshow p').each(function (i) {
$(this).delay(d*i).fadeIn('slow').delay(800).fadeOut('slow');
}).length;
setTimeout(loopMe, c * d);
}
loopMe();
});
You can give it a try here.
Instead of keeping up with a delay, you can just multiple it by the current index in the loop...since the first index is 0, the first one won't be delayed at all, then 2200ms times the amount of elements later, do the loop again. In the above code d is the delay, so it's easily adjustable, and c is the count of elements.
This solution is in my opinion more elegant, also more natural, it is easier to control, to correctly edit values of delays etc. I hope you'll like it.
$(document).ready(function () {
var elementsList = $('#slideshow p');
function animationFactory(i) {
var element = $(elementsList[i % elementsList.length]);
return function () {
element.delay(200).fadeIn('slow').delay(800).fadeOut('slow', animationFactory(i + 1));
};
}
animationFactory(0)();
});

Categories

Resources