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
JavaScript Chapter 6 Note new scoring. spin.js (page 67) function spin() function spin() { var obj_style = document.getElementById("d1").style; var obj_style.
Advertisements

function coffeeinfo() { document.getElementById('p3').innerHTML = "The word 'coffee' was.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
NAMEd anchors Enabling users to jump to specific points within Web documents.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
JavaScript Lesson 2 TBE 540 F. Fisher. Prerequisites  Before beginning this lesson, the learner must be able to… Create and edit a web page using a text.
Computer Science 103 Chapter 4 Advanced JavaScript.
Javascript Tools Document Object Model Problem Modeling & Solution Interaction Diagram Javascript Example quizzer06.html under Javascript-Examples > JavaScript-DOM.
 As you complete the assignment, go in order as the earlier bulleted parts provide an easier opportunity for points than the later.  This project is.
Review. What is wrong with the following function? imgArray = new Array() imgArray[0] = “mypic.jpg” imgArray[1] = “mypic2.jpg” imgArray[2] = “mypic3.jpg”
Web Programming Material From Greenlaw/Hepp, In-line/On-line: Fundamentals of the Internet and the World Wide Web 1 Introduction The JavaScript Programming.
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.,
INFSCI  Last time we built a doggie web page in class following the instructions in the slide deck: Build Web Page with HTML – see week 3 The topics.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
More JavaScript Examples Please use speaker notes for additional information!
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Internet Mark-up Languages CO32036 Part Lecture: Elementary JavaScript.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
1 Introduction to Javascript Peter Atkinson. 2 Objectives To understand and use appropriately some of the basic elements of Javascript: –alert and prompt.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 20 – Dynamic HTML: Path, Sequencer and Sprite ActiveX Controls Outline 20.1Introduction 20.2DirectAnimation.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
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,
Week 1 – Beginners Content McAfee & Big Fish Games CoderDojo.
 HTML is hypertext markup language. It is a way for people to display their information with more complex pictures and text displays. Before HTML, messages.
Dynamic HTML IV: Filters and Transitions Making Text glow glow filter –Add aura of color around text –Specify color and strength –Add padding.
JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.
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.
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.
CSS1 Dynamic HTML Objects, Collections & Events. CSS2 Introduction Dynamic HTML treats HTML elements as objects. Element’s parameters can be treated as.
Given the following code, what picture is displayed in the second image on the page when the first image is clicked on the first time? ___________________________.
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.
Basic Webpage Design Mark-up html document with images.
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.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 14 Key Concepts 1 Copyright © Terry Felke-Morris.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
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.
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.
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.
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.
InnerHTML: This is a paragraph about Ted the cat What is the innerHTML of ‘cat’? zombies
JavaScript and HTML Simple Event Handling 11-May-18.
Week 3: Introduction to Javascript
Looping SetTimeout is a loop
Week 4: Introduction to Javascript
JavaScript.
Chapter 14: DHTML: Event Model
Web Development & Design Foundations with HTML5 7th Edition
JavaScript and HTML Simple Event Handling 19-Sep-18.
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?
Understanding JavaScript and Coding Essentials
Event Driven Programming & User Defined Functions
Events Comp 205 Fall 2017.
Lesson 1: Decomposition
Functions, Regular expressions and Events
Pertemuan 1b
setTimeout() What if you want to automatically cycle through the pics?
Writing to the Page Formatting Forms
How to add a photo gallery in html/css
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

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(“function(){displaypic()}","2000") }

setTimeout(“function(){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 ( “function(){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: 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(“function(){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(“function(){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(“function(){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(“function(){displaypic()}",2000) } Different Styles link

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

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 3 var narr = new Array() narr[0] = “cat” narr[1] = “dog”  Now the array narr at location 1 holds the word “dog”, and we can use narr[1] as if it is the word “dog”  Parameters: click here to call the function with the parameter ‘easy’ function func(easyorhard) {If (easyorhard == ‘easy’) …  Now when you click on the paragraph, the word ‘easy’ is placed in the parameter easyorhard, so easyorhard can be used as if it is the word ‘easy’

Parameter Example: function showparam(param) { if (param == 'snow') {document.getElementById("h11").innerHTML = "it's snowing!" } else if (param == 'rain') {document.getElementById("h11").innerHTML = "it's raining!" } else if (param == 'sun') {document.getElementById("h11").innerHTML = "it's sunny!" } Different Styles click here for snow click here for rain click here for sun link

function changepic(par1) { document.getElementById('bigpic').src = par1; } function changebak() { document.getElementById('bigpic').src = ""; } <img src = "pic1.jpg" width = "40" height = "40" id = "img1" onMouseOver = "changepic('pic1.jpg')" onMouseOut = "changebak()"> <img src = "pic2.jpg" width = "40" height = "40" id = "img2" onMouseOver = "changepic('pic2.jpg')" onMouseOut = "changebak()"> <img src = "pic3.jpg" width = "40" height = "40" id = "img3" onMouseOver = "changepic('pic3.jpg')" onMouseOut = "changebak()"> <img src = "pic4.jpg" width = "40" height = "40" id = "img4" onMouseOver = "changepic('pic4.jpg')" onMouseOut = "changebak()"> link

var ycoord = 5 function myfunc(par1,par2) {document.getElementById(par1).style.position = "absolute" document.getElementById(par1).style.top = par2 + "px" ycoord = ycoord + Math.floor(Math.random()*100) document.getElementById(par1).style.left = ycoord+"px" setTimeout(function(){myfunc(par1,par2)},400) } function startfunc() {myfunc('img1',20) myfunc('img2',160) } <img src = "Images/train.jpg" width = "189" height = "267" id = "img1" style = "position: absolute; top: 20px; left: 5px"> <img src = "Images/train.jpg" width = "189" height = "267" id = "img2" style = "position: absolute; top: 160px; left: 5px"> link

Functions can have more than one parameter function myFunction(name,job) {document.getElementById('p1').innerHTML = "Welcome " + name + ", the " + job; } Results go here. Try it  Parameters match in order:  function myFunction( name, job)  onClick =myFunction(‘Harry Potter’,‘Wizard’)  onClick =myFunction(‘Bob’‘Builder’) link

var rightleft =0 function Movecar(direction) {if (direction == "right") {rightleft += 10 document.getElementById(“car").style.left = rightleft + "px" } else if (direction == "left") { rightleft -= 10 document.getElementById(“car").style.left = rightleft + "px" } What if we want to move the car over something? link Using Parameters to move things:

var rightleft =0 var xpos =0 function startit() { Movefrog() } function Movecar(direction) {if (direction == "right") {rightleft += 10 document.getElementById(“car").style.left = rightleft + "px" } else if (direction == "left") { rightleft -= 10 document.getElementById(“car").style.left = rightleft + "px" } function Movefrog() {xpos = Math.floor(Math.random() * 650) document.getElementById(‘frog').style.left = xpos + "px" setTimeout(“function(){Movefrog()}",20000) } </div link How do we check if the car is over the frog?

var rightleft =0 var xpos =0 Totalscore = 0 function startit() { Movefrog() } function Movecar(direction) {if (direction == "right") {rightleft += 10 document.getElementById(“car").style.left = rightleft + "px" } else if (direction == "left") { rightleft -= 10 document.getElementById(“car").style.left = rightleft + "px" } if ((rightleft > (xpos - 11)) && (rightleft < (xpos + 11)) ) {document.getElementById(‘frog').src="Images/splat.png" totalscore += 10 document.getElementById('tot').innerHTML = "Total Score: "+totalscore xpos = 0 } function Movefrog() {document.getElementById(‘frog').src="Images/frog.png“ xpos = Math.floor(Math.random() * 650) document.getElementById(‘frog').style.left = xpos + "px" setTimeout(“function(){Movefrog()}",20000) } Score goes here </div link

Back to our Game var count = 0 var total = 0 function myfunc() { if (count < 50) {var basetime = 1000 document.getElementById('img1').src = "gopher.jpg" document.getElementById('img1').style.position = "absolute" var ycoord = Math.floor(Math.random() * 800) var xcoord = Math.floor(Math.random() * 800) document.getElementById('img1').style.left = ycoord+"px" document.getElementById('img1').style.top = xcoord+"px" count = count + 1 var time = Math.floor(Math.random() * 250) + basetime setTimeout(“function(){myfunc()}",time) } else {document.getElementById('h11').innerHTML = “Congrats! You got "+ total+"!" } function func2() {total = total + 1 document.getElementById('h11').innerHTML = "You got " + total +"!" document.getElementById('img1').src = "Images/bang.png" } <img src = "Igopher.jpg" width = "189" height = "267" id = "img1" onMouseDown = "func2()"> link

Giving users the option of playing a harder version:  How? 1.Make the gopher get smaller? 2.Maybe make it faster? 3.You could also add distractions (other things flying randomly around) 4.Maybe even bombs that if you clicked on, the game would end immediately and you’d lose  Right now, we’ll worry about the first two…

Make the gopher change sizes randomly?:  I chose to alter the gopher’s size based on a percent.  So when I alter the gopher’s size, it will change proportionally var minpercent = 50 /* the smallest the image can get is 50% of full size */ So: var curpercent = Math.floor(Math.random() * 50) + minpercent curpercent will hold a random number between 0 and 1 now.  We must calculate the total width and height based on the percent var curwidth = Math.floor(curpercent/100 * 159) var curheight = Math.floor(curpercent/100* 227) /* 159 and 227 are the original width and height of the image*/  And finally, we must set our images width and height to those values: document.getElementById('img1').width = curwidth document.getElementById('img1').height = curheight

function changeGopherSize() { var minpercent = 50 /* we’re varying the image by 50% only */ var curpercent = Math.floor((Math.random() * 50) + minpercent) var curwidth = Math.floor(curpercent/100 * 159) var curheight = Math.floor(curpercent/100 * 227) /* 159 and 227 are the original width and height of the image */ document.getElementById('img1').width = curwidth document.getElementById('img1').height = curheight } <img src = "Images/gopher.jpg" width = "159" height = "227" id = "img1" onClick = "changeGopherSize()"> Can we add a parameter for the size? link

function changeGopherSize(minpercent) {var curpercent = Math.floor((Math.random() * 50) + minpercent) var curwidth = Math.floor(curpercent/100 * 159) var curheight = Math.floor(curpercent/100 * 227) /* 159 and 227 are the original width and height of the image*/ document.getElementById('img1').width = curwidth document.getElementById('img1').height = curheight } link

Adding size changes to game… var count = 0 var total = 0 function myfunc( minpercent ) { if (count < 50) {var basetime = 1000 /* smallest amount of time for setTimeout */ document.getElementById('img1').src = "gopher2.jpg" var ycoord = Math.floor(Math.random() * 800) var xcoord = Math.floor(Math.random() * 800) document.getElementById('img1').style.left = ycoord+"px" document.getElementById('img1').style.top = xcoord+"px“ var curpercent = Math.floor(Math.random() * 50) + minpercent var curwidth = Math.floor(curpercent/100 * 159) /* getting size in pixels */ var curheight = Math.floor(curpercent/100 * 227) document.getElementById('img1').width = curwidth /*setting width */ document.getElementById('img1').height = curheight /* setting height */ count = count + 1 var time = Math.floor(Math.random() * 250) + basetime setTimeout(“function(){myfunc()}",time) } else {document.getElementById('h11').innerHTML = "Congrats! You got " + total +"!" document.getElementById('img1').src = "Images/gameover.png" } Get the Gopher! Score so far: 0 Start Game (easy version) Start Game (hard version) link

Change speed to a parameter? var count = 0 var total = 0 function myfunc( minpercent ) { if (count < 50) {var basetime = 1000 /* smallest amount of time for setTimeout */ document.getElementById('img1').src = "gopher2.jpg" var ycoord = Math.floor(Math.random() * 800) var xcoord = Math.floor(Math.random() * 800) document.getElementById('img1').style.left = ycoord+"px" document.getElementById('img1').style.top = xcoord+"px“ var curpercent = Math.floor(Math.random() * 50) + minpercent var curwidth = Math.floor(curpercent/100 * 159) /* getting size in pixels */ var curheight = Math.floor(curpercent/100 * 227) document.getElementById('img1').width = curwidth /*setting width */ document.getElementById('img1').height = curheight /* setting height */ count = count + 1 var time = Math.floor(Math.random() * 250) + basetime setTimeout(“function(){myfunc()}",time) } else {document.getElementById('h11').innerHTML = "Congrats! You got " + total +"!" document.getElementById('img1').src = "Images/gameover.png" }

Change speed to a parameter? var count = 0 var total = 0 function myfunc( minpercent,basetime ) { if (count < 50) {document.getElementById('img1').src = "gopher2.jpg" var ycoord = Math.floor(Math.random() * 800) var xcoord = Math.floor(Math.random() * 800) document.getElementById('img1').style.left = ycoord+"px" document.getElementById('img1').style.top = xcoord+"px“ var curpercent = Math.floor(Math.random() * 50) + minpercent var curwidth = Math.floor(curpercent/100 * 159) /* getting size in pixels */ var curheight = Math.floor(curpercent/100 * 227) document.getElementById('img1').width = curwidth /*setting width */ document.getElementById('img1').height = curheight /* setting height */ count = count + 1 var time = Math.floor(Math.random() * 250) + basetime setTimeout(“function(){myfunc()}",time) } else {document.getElementById('h11').innerHTML = "Congrats! You got " + total +"!" document.getElementById('img1').src = "Images/gameover.png" } Get the Gopher! Score so far: 0 Start Game (easy version) Start Game (hard version) +