1

This is the code I'm using now to move a multi-layer carousel (left right up down) but I need to add a timeout function for say 5000 so the animation has time to complete before the next click is triggered.

function checkKey(e){

       switch (e.keyCode) {
          case 40:
              //alert("down");
              $("#down").trigger("click");
              break;
          case 38:
              //alert("up");
              $("#up").trigger("click");
              break;
          case 37:
              //alert("left");
              $("#left").trigger("click");
              break;
          case 39:
              //alert("right");
              $("#right").trigger("click");
              break;  
              }      
      }

     // Call checkKey on key press
     if ($.browser.mozilla) {
         $(document).keypress(checkKey);
     } else {
         $(document).keydown(checkKey);
     }

UPDATE:

I ended up finding a solution. I know there is probably a much cleaner way to do this but I don't know how so forgive my messiness.

// Set Time variable
   var checkTime = 0;

   // Call checkKey on key press
   if ($.browser.mozilla) {
       $(document.documentElement).keypress(function (event) {

          // set variable to current time
          var currentTime = new Date()
          // See if Now's current time is 400 past the old current time
          if((currentTime.getTime() - checkTime) > 400){

                // If so then fire the triggers.
                // Using if seemed to limit the inputs to a single keystroke        
                if (event.keyCode == 40) {
                        $("#down").trigger("click");
                } else if (event.keyCode == 38) {
                        $("#up").trigger("click");
                } else if (event.keyCode == 37) {
                        $("#left").trigger("click");
                } else if (event.keyCode == 39) {
                        $("#right").trigger("click");
                }
              // Reset current time.
              checkTime =currentTime.getTime();
          }


        });
   } else {
       $(document.documentElement).keydown(function (event) {

          // set variable to current time
          var currentTime = new Date()
          // See if Now's current time is 400 past the old current time
          if((currentTime.getTime() - checkTime) > 400){

                // If so then fire the triggers.
                // Using if seemed to limit the inputs to a single keystroke        
                if (event.keyCode == 40) {
                        $("#down").trigger("click");
                } else if (event.keyCode == 38) {
                        $("#up").trigger("click");
                } else if (event.keyCode == 37) {
                        $("#left").trigger("click");
                } else if (event.keyCode == 39) {
                        $("#right").trigger("click");
                }
              // Reset current time.
              checkTime =currentTime.getTime();
          }


        });
   }
3
  • This is going to annoy users ... they press a key and wont get the expected result - why not use stop() to complete the animation and start the next ? Commented Mar 29, 2012 at 15:47
  • I would go more toward a delay than a "lock", as it would be easier to implement and because it would seriously annoy me if I was trying to jump ahead 3 pictures and had to wait for each turn before the next keypress. Commented Mar 29, 2012 at 15:48
  • I guess the issue is that I'm moving around an .active (ul.active) and .active-li (ul.active li.active-li) to track the current slide's position. And it's a combination bumping those closes around and at some point if the users taps the arrows too quickly it gets ahead of itself. Commented Mar 29, 2012 at 15:57

2 Answers 2

1

try using delay function from the jquery library here is the tutorial url http://api.jquery.com/delay/

it probably would be something like this.

 $("#down").delay(5000).trigger("click");
Sign up to request clarification or add additional context in comments.

1 Comment

Should that replace this - $("#down").trigger("click");
0

This should do the trick:

setTimeout(function() { 
   $("#right").trigger("click");
}, 5000);

1 Comment

More context. Where does this go? What event is it bound to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.