Skip to content

Instantly share code, notes, and snippets.

@Wolfr
Created November 3, 2011 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wolfr/1337697 to your computer and use it in GitHub Desktop.
Save Wolfr/1337697 to your computer and use it in GitHub Desktop.
jwerty.key('←', function () {
// Check if we are on the first slide, if so, do not execute
if (!$('.introSlide').is(":visible")) {
prevSlide();
buttonToggle();
};
});
$('#prevSlide').click(function() {
// Check if we are on the first slide, if so, do not execute
if (!$('.introSlide').is(":visible")) {
prevSlide();
buttonToggle();
};
});
@decthomas
Copy link

You could put the ! .introSlide visible check into a separate function.
That way you can call that function on key change or on click instead of repeating the code twice.

@joggink
Copy link

joggink commented Nov 3, 2011

  jwerty.key('←', function () {
    showNext();
  });

  $('#prevSlide').click(function() {
    showNext();
  });

function showNext(){
    // Check if we are on the first slide, if so, do not execute
    if (!$('.introSlide').is(":visible")) {
      prevSlide();
      buttonToggle();
    }
}

@vormplus
Copy link

vormplus commented Nov 3, 2011

jwerty.key('←', function () {
checkFirstSlide()
});

$('#prevSlide').click(function() {
checkFirstSlide()
});

// Check if we are on the first slide, if so, do not execute
function checkFirstSlide() {
if (!$('.introSlide').is(":visible")) {
prevSlide();
buttonToggle();
}
}

@Wolfr
Copy link
Author

Wolfr commented Nov 3, 2011

I wanted to do something like to make it elegant. WARNING! BOGUS CODE!

jwerty.key('←'),  $('#prevSlide').click, function () {
    // Check if we are on the first slide, if so, do not execute
    if (!$('.introSlide').is(":visible")) {
      prevSlide();
      buttonToggle();
    };
});

But functions it is then... bah

@joggink
Copy link

joggink commented Nov 3, 2011

Not sure if you can combine a click and a keypress event like that...

@joggink
Copy link

joggink commented Nov 3, 2011

  jwerty.key('←', showNext);
  $('#prevSlide').click(showNext);

function showNext(){
    // Check if we are on the first slide, if so, do not execute
    if (!$('.introSlide').is(":visible")) {
      prevSlide();
      buttonToggle();
    }
}

@Wolfr
Copy link
Author

Wolfr commented Nov 3, 2011

So we can remove the anonymous functions say eising

Example:

jwerty.key('↑', function () {
  resetSlides();
});

$('.resetSlides').click(function() {
  resetSlides();
});

How?

(NM you were faster :))

@mathiasbynens
Copy link

jwerty.key('↑', function () {
  resetSlides();
});

$('.resetSlides').click(function() {
  resetSlides();
});

Could be just…

jwerty.key('↑', resetSlides);

$('.resetSlides').click(resetSlides);

But actually you don’t need a named function at all:

var $resetSlides = $('.resetSlides');

$resetSlides.click(function() {
  // teh coads
});

jwerty.key('↑', $resetSlides.click); // i.e. just trigger a `click` event on `$resetSlides`

@Wolfr
Copy link
Author

Wolfr commented Nov 3, 2011

Hmm yeah, that makes sense. Your 2nd code block is more readable and I can keep all my functions apart so went for that. The 3rd code block is more 1337 I guess ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment