Skip to content
Browse files

prevent window load event from happening too fast

  • Loading branch information...
1 parent d6cb217 commit f3a9309bda5cca3cc0b147a3b4aa52a9794488bc @chriscoyier chriscoyier committed Mar 31, 2011
Showing with 42 additions and 27 deletions.
  1. +42 −27 scripts/slide.js
View
69 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.
Something went wrong with that request. Please try again.