SlideShare a Scribd company logo
EVENTS + TIMERS
AGENDA

• Mouse Events
• Keyboard Events
• Form Events
• .bind(), .unbind(), and .on() functions
• Timers




2
MOUSE EVENTS

• mousedown
    –Mouse button is pressed over the element
• mouseup
    –Mouse button is released over the element
• click
    –User has pressed and released the mouse button




3
MOUSE EVENTS

• mousemove
    –user has moved the mouse over the element
• All mouse events give you position on the
  mouse
    – event.pageX
    – event.pageY




4
MOUSE EVENTS



$(“body”).mousemove(function(event) {
  var x = event.pageX;
  var y = event.pageY;
  console.log(“Mouse at”, x, y);
});




5
PREVENTING DEFAULT EVENT BEHAVIOR




$(“body”).click(function(event) {
  event.preventDefault();
});




6
EXPERIMENT - MOUSE EVENTS




7
KEYBOARD EVENTS
• keydown
    –Key has been pressed
• keyup
    –Key has been released
• keypressed
    –Key has been “pressed” (up and down)
• event.which
    –let’s us know what key is being pressed
    –http://asquare.net/javascript/tests/KeyCode.html
8
KEYBOARD EVENTS



$(“#test-input”).keyup(function(event) {
 if(event.which == 13) {
   alert(“Pressed Enter!”);
 }
});




9
EXPERIMENT - KEYBOARD EVENTS




10
FORM EVENTS

• submit
     –Form has been submitted (user clicked “submit” /
       pressed return)
     – Remember event.preventDefault()!
• change
     –The user changed something in the element
• select
     –User has selected a new choice in an element


11
FORM EVENTS

• focus
     –User has placed his cursor on an element or the
      element is selected
• blur
     –The element has lost focus (eg. user has clicked
      somewhere else).




12
PREVENTING DEFAULT EVENT BEHAVIOR




$(“form”).sumbit(function(event) {
  event.preventDefault();
  // form will now not POST / redirect
  // so you can use the data!
});




13
BINDING EVENTS

• We’ve been using shortcuts
     –You can see them all at http://api.jquery.com/
      category/events
• .bind and .on allow you to bind any event
  your want.




14
.BIND - SAME THING!
 $(“body”).mousemove(function(event) {
   var x = event.pageX;
   var y = event.pageY;
   console.log(“Mouse at”, x, y);
 });


 $(“body”).bind(“mousemove”, function(event) {
   var x = event.pageX;
   var y = event.pageY;
   console.log(“Mouse at”, x, y);
 });

15
.ON LET’S YOU DO MULTIPLE AT ONCE



 $(“body”).on(“mousedown mouseup”, function(event) {
   var x = event.pageX;
   var y = event.pageY;
    console.log(“Mouse at”, x, y);
 });




16
TIMERS

• Timers let us execute code after a period of
  time, or over and over again at a set interval.

• setTimeout(function, delay);
     –Execute a function after a delay
• setInterval(function, delay);
     –Execute a function continously after a delay



17
TIMERS

• When a timer is set, a timer id is returned. We
  can use this to stop the timer.

• clearTimeout(id);
• clearInterval(id);




18
TIMERS - TIMEOUT EXAMPLE
 function timetest() {
   console.log(“1 second has passed!”);
 }

 var timerId = setTimeout(timetest, 1000);




19
TIMERS - INTERVAL EXAMPLE
 function timetest() {
   console.log(“1 second has passed!”);
 }

 var timerId = setInterval(timetest, 1000);



 // Will print forever! We can stop it with:
 clearInterval(timerId);




20
NOW LET’S MAKE A COLOR PICKER




21
22

More Related Content

PDF
Yahoo presentation: JavaScript Events
DOC
KODE JS POKENNNNN
PDF
The Pied Piper of Selenium
PDF
Touchevents
KEY
YUI for Mobile - HTML5DevConf 11
KEY
Yui mobile
PPTX
Leveraging jQuery's Special Events API (JSConf 2012)
PDF
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Yahoo presentation: JavaScript Events
KODE JS POKENNNNN
The Pied Piper of Selenium
Touchevents
YUI for Mobile - HTML5DevConf 11
Yui mobile
Leveraging jQuery's Special Events API (JSConf 2012)
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...

Similar to ev (20)

PDF
Voices That Matter: JavaScript Events
PDF
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
PPTX
jQuery for Beginners
PPT
JavaScript: Events Handling
PPT
Flash Lite & Touch: build an iPhone-like dynamic list
PDF
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
KEY
Sbaw091117
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
PDF
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
PDF
Chapt 04 user interaction
PDF
Events.pdf
PDF
Asynchronous Programming at Netflix
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
KEY
Events
KEY
Advanced jQuery
KEY
Leaving Flatland: getting started with WebGL
PDF
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
PDF
Introduction to CQRS and Event Sourcing
Voices That Matter: JavaScript Events
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
jQuery for Beginners
JavaScript: Events Handling
Flash Lite & Touch: build an iPhone-like dynamic list
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Sbaw091117
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
Chapt 04 user interaction
Events.pdf
Asynchronous Programming at Netflix
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
Events
Advanced jQuery
Leaving Flatland: getting started with WebGL
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Introduction to CQRS and Event Sourcing
Ad

ev

  • 2. AGENDA • Mouse Events • Keyboard Events • Form Events • .bind(), .unbind(), and .on() functions • Timers 2
  • 3. MOUSE EVENTS • mousedown –Mouse button is pressed over the element • mouseup –Mouse button is released over the element • click –User has pressed and released the mouse button 3
  • 4. MOUSE EVENTS • mousemove –user has moved the mouse over the element • All mouse events give you position on the mouse – event.pageX – event.pageY 4
  • 5. MOUSE EVENTS $(“body”).mousemove(function(event) { var x = event.pageX; var y = event.pageY; console.log(“Mouse at”, x, y); }); 5
  • 6. PREVENTING DEFAULT EVENT BEHAVIOR $(“body”).click(function(event) { event.preventDefault(); }); 6
  • 8. KEYBOARD EVENTS • keydown –Key has been pressed • keyup –Key has been released • keypressed –Key has been “pressed” (up and down) • event.which –let’s us know what key is being pressed –http://asquare.net/javascript/tests/KeyCode.html 8
  • 9. KEYBOARD EVENTS $(“#test-input”).keyup(function(event) { if(event.which == 13) { alert(“Pressed Enter!”); } }); 9
  • 11. FORM EVENTS • submit –Form has been submitted (user clicked “submit” / pressed return) – Remember event.preventDefault()! • change –The user changed something in the element • select –User has selected a new choice in an element 11
  • 12. FORM EVENTS • focus –User has placed his cursor on an element or the element is selected • blur –The element has lost focus (eg. user has clicked somewhere else). 12
  • 13. PREVENTING DEFAULT EVENT BEHAVIOR $(“form”).sumbit(function(event) { event.preventDefault(); // form will now not POST / redirect // so you can use the data! }); 13
  • 14. BINDING EVENTS • We’ve been using shortcuts –You can see them all at http://api.jquery.com/ category/events • .bind and .on allow you to bind any event your want. 14
  • 15. .BIND - SAME THING! $(“body”).mousemove(function(event) { var x = event.pageX; var y = event.pageY; console.log(“Mouse at”, x, y); }); $(“body”).bind(“mousemove”, function(event) { var x = event.pageX; var y = event.pageY; console.log(“Mouse at”, x, y); }); 15
  • 16. .ON LET’S YOU DO MULTIPLE AT ONCE $(“body”).on(“mousedown mouseup”, function(event) { var x = event.pageX; var y = event.pageY; console.log(“Mouse at”, x, y); }); 16
  • 17. TIMERS • Timers let us execute code after a period of time, or over and over again at a set interval. • setTimeout(function, delay); –Execute a function after a delay • setInterval(function, delay); –Execute a function continously after a delay 17
  • 18. TIMERS • When a timer is set, a timer id is returned. We can use this to stop the timer. • clearTimeout(id); • clearInterval(id); 18
  • 19. TIMERS - TIMEOUT EXAMPLE function timetest() { console.log(“1 second has passed!”); } var timerId = setTimeout(timetest, 1000); 19
  • 20. TIMERS - INTERVAL EXAMPLE function timetest() { console.log(“1 second has passed!”); } var timerId = setInterval(timetest, 1000); // Will print forever! We can stop it with: clearInterval(timerId); 20
  • 21. NOW LET’S MAKE A COLOR PICKER 21
  • 22. 22

Editor's Notes