msg.length) pos = 0; window.setTimeout("ScrollIt()",200); } Repeat every fifth of a second Concept: display from middle to end, and then from front to middle"> msg.length) pos = 0; window.setTimeout("ScrollIt()",200); } Repeat every fifth of a second Concept: display from middle to end, and then from front to middle">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loops Pretest var i=1, sum=0; while (i<5) { sum += i++; } alert(sum); Counter controlled var i=0, sum=0; for (i=1; i<=5; i++) sum += I; alert(sum); Post-test.

Similar presentations


Presentation on theme: "Loops Pretest var i=1, sum=0; while (i<5) { sum += i++; } alert(sum); Counter controlled var i=0, sum=0; for (i=1; i<=5; i++) sum += I; alert(sum); Post-test."— Presentation transcript:

1 Loops Pretest var i=1, sum=0; while (i<5) { sum += i++; } alert(sum); Counter controlled var i=0, sum=0; for (i=1; i<=5; i++) sum += I; alert(sum); Post-test loop var i=0, sum=0; do { sum += i++; } while (i<=5); alert(sum); Questions –Print "Hello" 100 times –Count backwards from 100 to 1 –Lets do some examples in the book Computers often need to repeat the same thing over and over

2 Move the Mouse div {position:absolute; left:0px; top:100px; width:100px; height:100px; } var position=-95; var speed = Math.floor(Math.random()*10)+2; function next() { position+=speed; if (position>795) position=-95; document.getElementById("mouse").style.left=position + 'px'; window.setTimeout("next();",10); } Animation A Moving Mouse Modify the position of the tag and repeatedly call the next() function every 10 milliseconds using calls to setTimeout()

3 A Scrolling Message var middle = 0; var msg = "This is an example of a scrolling message. "; function ScrollIt() { var text = msg.substring(middle, msg.length) +msg.substring(0,middle); var td = document.getElementById("scroll"); td.innerHTML = text; if (++pos > msg.length) pos = 0; window.setTimeout("ScrollIt()",200); } Repeat every fifth of a second http://cs.sou.edu/~harveyd/classes/cs210/examples/week9/scroll.htm Concept: display from middle to end, and then from front to middle

4 Review Questions 1.What are the three kind of loops? How do you determine which to use? 2.How do you create an array of strings? 3.How do you change the text content of a tag? 4.How do you find the element with attribute "id='scroll'"? 5.What are three examples where arrays are useful? 6.How does the setTimeout() function work? 7.What is a scrolling message? How do you create such a message using JavaScript and HTML? 8.How do you use a loop to cause an image to move in a browser window?


Download ppt "Loops Pretest var i=1, sum=0; while (i<5) { sum += i++; } alert(sum); Counter controlled var i=0, sum=0; for (i=1; i<=5; i++) sum += I; alert(sum); Post-test."

Similar presentations


Ads by Google