Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery fade

http://codepen.io/tushar_13/full/dMRewy/

Can someone please take a look at my code, When I click the "Surprise Me" button for a new quote, fade function is behaving absurdly.

What I mean by absurd is When I click the button for a new quote, it first shows the next quote for just a second and then performs some fade stuff!

What I want is when I click the button, the quote on the page fades out and the new quote fades in...

What should I do?? Any help?

Line 51(js file)

4 Answers

I changed it so that the function quoteGen() had same delay of the fadeIn/fadeOut methods. Try it like this

$(".btn").on("click", function () {
    $(".quotebox").fadeOut(1000);
    setTimeout(function() { quoteGen(); }, 1000);
    $(".quotebox").fadeIn(1000);
});

ok it works! thanks but what is happening really in this code?

Correct me If I am wrong..

First the element fades out, then we are restricting the function quoteGen to take a pause for 1sec and then after 1 sec, the text fades in because the function starts running again.

I've changed your jQuery code at line 50. Like this it should go better

$(".btn").on("click", function () {
    $(".quotebox").hide();
    $(".quotebox").fadeIn(1000);
    quoteGen();

});

Thanks man! So it first hides the quote and then displays it with a fading effect 1000ms.

But what if I want to fade it out as well?

And my second question is How do I use API for generating quotes??

I have no idea how to do that!