Link. setTimeout() What if you want to automatically cycle through the pics? So you don’t have to keep clicking the button to see the next pic Use the.

Slides:



Advertisements
Similar presentations
function coffeeinfo() { document.getElementById('p3').innerHTML = "The word 'coffee' was.
Advertisements

Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
1 JavaScript Document Object Model & Events. 2 Document Object Model (DOM) JavaScript access to the elements of an HTML document. An object hierarchy.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JavaScript Part 1.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
JavaScript Informatics for economists I. Introduction Programming language used in web pages. Simple and easy to use – written in HTML document. Client.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
JavaScript, jQuery, and Mashups Incorporating JavaScript, jQuery, and other Mashups into existing pages.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Game var count = 0 var total = 0 function myfunc() { if (count < 50) {var basetime = 1000 document.getElementById('img1').src = "Images/gopher.jpg" document.getElementById('img1').style.position.
44238: Dynamic Web-site Development Client Side Programming Ian Perry Room:C48 Extension:7287
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
Handling Image & Animation Using JavaScript HTML tag to display image: More options:
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
Javascript JavaScript is what is called a client-side scripting language:  a programming language that runs inside an Internet browser (a browser is also.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
EIW: Javascript DOM and Events1 JavaScript Document Object Model & Events.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
The Web Browser Button Game Click to play Click to play.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
SetTimeout()  What if you want to automatically cycle through the pics?  So you don’t have to keep clicking the button to see the next pic  Use the.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
Tutorial 11 1 JavaScript Operators and Expressions.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Event Handlers Events are asynchronous. When an event occurs, JavaScript can execute code in response to the user’s action. This response to the user-initiated.
Loops Pretest var i=1, sum=0; while (i
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
Changing CSS Styles via JavaScript No readings?. 2 Review? You can change the CSS styling of an element with JavaScript Syntax is similar to accessing.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Third lecture Event 27/2/2016 JavaScript Tutorial.
InnerHTML: This is a paragraph about Ted the cat What is the innerHTML of ‘cat’? zombies
Week 3: Introduction to Javascript
Looping SetTimeout is a loop
HTML & teh internets.
Week 4: Introduction to Javascript
JavaScript - Errors & Exceptions Handling
JavaScript.
JavaScript (JS) Basics
Chapter 14: DHTML: Event Model
Web Development & Design Foundations with HTML5 7th Edition
Parameters: Parameters are another way of having something hold a value. E.g., var x = 3 Now the variable x holds 3. We can use x as if it is the number.
setTimeout() What if you want to automatically cycle through the pics?
Event Driven Programming & User Defined Functions
Events Comp 205 Fall 2017.
JavaScript Defined General Syntax Body vs. Head Variables Math & Logic
Functions, Regular expressions and Events
Web Design and Development
setTimeout() What if you want to automatically cycle through the pics?
Code Topic 9 Standard JavaScript Events Laura Friedman
JavaScript and Ajax (JavaScript Events)
Week 5: Recap and Portfolio Site
Web Programming and Design
Presentation transcript:

link

setTimeout() What if you want to automatically cycle through the pics? So you don’t have to keep clicking the button to see the next pic Use the built-in JavaScript setTimeout() function function displaypic() { num = num + 1 if (num >= picArray.length) { num = 0 } document.getElementById("pic1").src = picArray[num] document.getElementById("p1").innerHTML = num setTimeout("displaypic()","2000") }

setTimeout("displaypic()","2000") setTimeout Calls the function setTimeout, which causes javascript to STOP running – just freeze! It stops for the number specified (in milliseconds) After that many milliseconds, it calls the function specified So in the above example, setTimeout freezes javascript for 2000 milliseconds (or 2 seconds), and then after 2 seconds, it calls the function displaypic(), just as if you’d clicked on a button calling it.

Example: function setToRed ( ) {document.getElementById("colorButton").style.color = "#FF0000"; setTimeout ( "setToBlack()", 2000 ); } function setToBlack ( ) {document.getElementById("colorButton").style.color = "#000000"; } <input type="button" name="clickMe" id="colorButton" value="Click me and wait!" onclick="setToRed()"/> Link:

var count = 0 function myfunc() {if (count == 1) {count = 0 document. getElementById(“img1”).src = " " } else if (count == 0) { count = 1 document. getElementById(“img1”).src = "Images/lightbulb.jpg" } setTimeout("myfunc()",1000) } Link:

Example: var picArray = new Array() picArray[0]="Images/ kittyfur-back.jpg " picArray[1]=“Images/kittyhimself.jpg" picArray[2]="Images/kittybreakfast.jpg" picArray[3]="Images/kittyno-regrets.jpg" picArray[4]="Images/kttylemon.jpg" var num = -1 function displaypic() { num = num + 1 if (num >= picArray.length) { num = 0 } document.getElementById("pic1").src = picArray[num] document.getElementById("p1").innerHTML = num setTimeout("displaypic()","2000") } Vacation Pics Image number link

Starting automatically… So far we’ve started function in a couple of ways: Primarily: onClick Also: onMouseOver and onMouseOut If we want a function to start automatically, without our having to click or run our mouse over anything… We can use onLoad We’ll use it within the body tag, e.g., This means that when the body of your html page loads into the browser, func() will be executed.

So now we’d have: var count = 0 function myfunc() {if (count == 1) {count = 0 document. getElementById(“img1”).src = " " } else if (count == 0) { count = 1 document. getElementById(“img1”).src = "Images/lightbulb.jpg" } setTimeout("myfunc()",1000) } link

What about this? var picArray = new Array() picArray[0]="Images/Images/bg1.jpg" picArray[1]="Images/Images/bg2.jpg" picArray[2]="Images/Images/bg3.jpg" picArray[3]="Images/Images/bg4.jpg" picArray[4]="Images/Images/bg5.jpg“ var clrArray = new Array() clrArray[0]="#FFFF66" clrArray[1]="#FF0033" clrArray[2]="#FFFFFF" clrArray[3]="#112200" clrArray[4]="#88AA44“ var num = -1 function displaypic() { num = num + 1 if (num >= picArray.length) { num = 0 } document.getElementById("h11").style.background = "url("+picArray[num]+")" document.getElementById("h11").style.color = clrArray[num] setTimeout("displaypic()",2000) } Different Styles link

What does this do? var count = 0 var ycoord = 800 function myfunc() { if (count == 50) {count = 0 ycoord = 800 document.getElementById('img1').style.position = "absolute" } else {document.getElementById('img1').style.position = "absolute" ycoord = ycoord - 10 document.getElementById('img1').style.left = ycoord+"px" count = count + 1 } setTimeout("myfunc()",400) } link

Game var count = 0 var ycoord = 0 var xcoord = 0 var total = 0 function myfunc() { if (count == 50) { count = 0 xcoord = 0 ycoord = 0 document.getElementById('h11').innerHTML = "Congratulations! You got " + total +"!" } else {document.getElementById('img1').src = "Images/gopher.jpg" document.getElementById('img1').style.position = "absolute" ycoord = Math.floor(Math.random() * 800) xcoord = Math.floor(Math.random() * 800) document.getElementById('img1').style.left = ycoord+"px" document.getElementById('img1').style.top = xcoord+"px" count = count + 1 time = Math.floor(Math.random() * 250) setTimeout("myfunc()",time) } function func2() { total = total + 1 document.getElementById('h11').innerHTML = "You got " + total +"!" document.getElementById('img1').src = "Images/bang.png" } link