views:

370

answers:

1

Hello,

I have a spash screen on a website that has a div with the ID of "splash" i'm trying to make the div fade in then if the user clicks on the div it fades out and redircts to the main site. If the user dosen't click it just fades out and redirects after 10 seconds.

The timed redirect is working but not the click function.

    <script type="text/javascript">
  $(document).ready(function() {
  $('#splash').hide();  
        $('#splash').fadeIn(1000, function() {
              $(this).delay(10000).fadeOut(1000, function() { 
               window.location = 'http://www.examle.com'; });
              $(this).click().fadeOut(1000,function() { 
               window.location = 'http://www.example.com'; });
         });
  });
</script>

Any help would be great

+1  A: 

Try this:

$(document).ready(function() {
  $('#splash').hide();
  $('#splash').click(function(){
             $(this).fadeOut(1000,function() { 
                     window.location = 'http://www.example.com'; });
             });
  $('#splash').fadeIn(1000, function() {
           window.setTimeout ( function() {
             $('#splash').fadeOut(1000, function() { 
               window.location = 'http://www.examle.com'; }) }
             , 10000);
     });

 });​

Changes that I've made: I've moved setting the click handler outside the fadeOut function (better practice, IMHO) and I've changed your call to delay() to a setTimeout().

The difference is, delay() will not allow other jQuery code to be executed in the background, while setTimeout() will.

Josiah
Thanks, the click function still doesn't work with the above code.
BandonRandon
Sorry, the error was not what I intially thought. Try the updated code sample :)
Josiah
The problem is that it's not stoping `delay()` on click. So if you click you still have to wait till the delay is over. Weird. (But it looks like you discoverd that :))
BandonRandon
Try the updated code I posted (I edited my answer). It worked the way it was supposed to for me. Like I mentioned in my updated answer, I traded delay() which does not allow further jQuery code to execute, for setTimeout() which will.
Josiah
That's also not working, It's running the fadein then the fadeout without taking the timeout into effect.
BandonRandon
Okay. This time it works, I promise :D I've wrapped the JQuery call inside a function, which is required in this case because it's a multi-call, rather than a single function call. Interesting... I learned something new :) Again, see the code I've posted in my answer.
Josiah
Thank you soo much! It did work this last time! Thanks for all your help I hope I wasn't too annoying ;)
BandonRandon
You're very welcome. And not at all :) I'm happy to have been able to help.
Josiah