Nick Maddren
Nick Maddren

Reputation: 583

Using click() and setTimeout() jQuery

Hey guys I am just wondering what would be the best way to make my jQuery function a little bit more DRY.

Here is an example of my code:

    setTimeout(function(){
        $(".landing-page-header-container").addClass('reveal-signup');
        $(".landing-page-vehicle-left-container").addClass('animated fadeInLeftBig');
        $(".landing-page-vehicle-right-container").addClass('animated fadeInUpBig');
        $(".landing-close-video").removeClass('reveal-landing-close-video');
      },8000)
    });
    $('.landing-close-video').click(function(){
      $(".landing-page-header-container").addClass('reveal-signup');
      $(".landing-page-vehicle-left-container").addClass('animated fadeInLeftBig');
      $(".landing-page-vehicle-right-container").addClass('animated fadeInUpBig');
      $(".landing-close-video").removeClass('reveal-landing-close-video');
    });

Now as you can see I am repeating myself quite a bit here because the setTimeout function is doing exactly the same thing as the click function.

Is there anyway I can combine these two functions?

Thanks, Nick

Upvotes: 1

Views: 47

Answers (1)

Quentin
Quentin

Reputation: 944455

Use a variable.

function do_stuff() {
  $(".landing-page-header-container").addClass('reveal-signup');
  $(".landing-page-vehicle-left-container").addClass('animated fadeInLeftBig');
  $(".landing-page-vehicle-right-container").addClass('animated fadeInUpBig');
  $(".landing-close-video").removeClass('reveal-landing-close-video');
}

setTimeout(do_stuff, 8000);
$('.landing-close-video').click(do_stuff);

Upvotes: 1

Related Questions