Skip to content

Commit

Permalink
prevent window load event from happening too fast
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscoyier committed Mar 31, 2011
1 parent d6cb217 commit f3a9309
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions scripts/slide.js
@@ -1,29 +1,44 @@
$(window).load(function() {
$("html").addClass("loaded");
});

function gotoThere(link) {
window.location = link;
}

$("#previous, #next").click(function(e) {
$("html").addClass("leaving");
setTimeout(gotoThere, 1500, this.href);
return false;
});

$("#jumper").change(function() {
$("html").addClass("leaving");
setTimeout(gotoThere, 1500, $(this).val());
});

$(document).keydown(function(e) {
// right arrow
if (e.keyCode == 39) {
$("#next").click();
}
// left arrow
if (e.keyCode == 37) {
$("#previous").click();
$(function() {

// Prevent window.load from happening too fast
// Idea by Chris Coyier and debungled by Paul Irish
(function fn() {
fn.now = +new Date;

$(window).load(function() {
// 500ms seems to be enough... feel free to adjust per machine
if (+new Date - fn.now < 500) setTimeout(fn, 500);
$("html").addClass("loaded");
});

})();

// Simple redirect function
function gotoThere(link) {
window.location = link;
}

$("#previous, #next").click(function(e) {
$("html").addClass("leaving");
setTimeout(gotoThere, 1500, this.href);
return false;
});

$("#jumper").change(function() {
$("html").addClass("leaving");
setTimeout(gotoThere, 1500, $(this).val());
});

// Left and right arrow keys for changing slides
$(document).keydown(function(e) {
// right arrow
if (e.keyCode == 39) {
gotoThere($("#next").attr("href"));
}
// left arrow
if (e.keyCode == 37) {
gotoThere($("#previous").attr("href"));
}
});

});

0 comments on commit f3a9309

Please sign in to comment.