SlideShare una empresa de Scribd logo
1 de 22
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

Más contenido relacionado

Similar a ev

Voices That Matter: JavaScript Events
Voices That Matter: JavaScript EventsVoices That Matter: JavaScript Events
Voices That Matter: JavaScript EventsPeter-Paul Koch
 
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Patrick Lauke
 
jQuery for Beginners
jQuery for Beginners jQuery for Beginners
jQuery for Beginners NAILBITER
 
Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listSmall Screen Design
 
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефонаКурсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефонаГлеб Тарасов
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloRobert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoRobert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileRobert Nyman
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresRobert Nyman
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interactionEdi Faizal
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at NetflixC4Media
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresRobert Nyman
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuerysergioafp
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLgerbille
 
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013Patrick Lauke
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingSamuel ROZE
 

Similar a ev (20)

Voices That Matter: JavaScript Events
Voices That Matter: JavaScript EventsVoices That Matter: JavaScript Events
Voices That Matter: JavaScript Events
 
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
 
jQuery for Beginners
jQuery for Beginners jQuery for Beginners
jQuery for Beginners
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
Flash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic listFlash Lite & Touch: build an iPhone-like dynamic list
Flash Lite & Touch: build an iPhone-like dynamic list
 
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефонаКурсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
Курсы по мобильной разработке под iOS. 4 лекция. Возможности телефона
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
Sbaw091117
Sbaw091117Sbaw091117
Sbaw091117
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
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 - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
 
Chapt 04 user interaction
Chapt 04 user interactionChapt 04 user interaction
Chapt 04 user interaction
 
Events.pdf
Events.pdfEvents.pdf
Events.pdf
 
Asynchronous Programming at Netflix
Asynchronous Programming at NetflixAsynchronous Programming at Netflix
Asynchronous Programming at Netflix
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
 
Events
EventsEvents
Events
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuery
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
Getting touchy - an introduction to touch events / Sud Web / Avignon 17.05.2013
 
Introduction to CQRS and Event Sourcing
Introduction to CQRS and Event SourcingIntroduction to CQRS and Event Sourcing
Introduction to CQRS and Event Sourcing
 

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

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n