簡體   English   中英

JavaScript 使用 setInterval 更改圖像和計數器

[英]JavaScript changing an image and counter with setInterval

因此,我正在使用 JavaScript 進行家庭作業,我遇到的問題是創建 changeAd() 函數以在 5 秒后用函數中的其他圖像之一替換表中的起始圖像。 我相當迷茫,因此非常感謝任何幫助或指示! 這是我到目前為止:

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>CVR1</title> </head> <body onLoad="startAdPage()"> <script type="text/javascript"> /* <![CDATA[ */ function startAdPage() { setInterval(changAd(),5000); setInterval(startCountdown(),1000); } function changeAd() { THIS NEEDS WORK should change the image every 5 seconds to replace the one in the table, i think this needs to be an array/list cvb1.gif; cvb2.gif; cvb3.gif; } var count() = 15; function startCountdown() { while count >= 0; THIS NEEDS WORK should change the value of textfield to countdown from 15 to 0 decrease by one each time it executes, when count reaches 0 clear both intervals and redirect browser to CVR2.html } /* ]]> */ </script> <table> <tr> <td><img src="cvb1.gif"></td> <td><p>Advertisement</p><p>The Central Vally Realtors home page will be displayed in TEXTFIELD seconds.</p><p><a href="CVR2.html">Skip Advertisement</a></p></td> </tr> </table> </body> </html>

First thing you should remove parentheses from the call to your functions in setInterval.
Secondly I assume you know how to declare an array to save names of your images, after this is done you can go here http://stackoverflow.com/questions/5915096/get-random-item-from-javascript-array to see how to get a random element from this array.
And finally to change the image in javascript you should refer here http://stackoverflow.com/questions/7312553/change-image-source-with-javascript.
I hope this helped.

這是我要使用的最終工作代碼,我認為我沒有正確使用 startCountdown 函數中指向的文本字段,但它可以工作。

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>CVR1</title> </head> <body onLoad="startAdPage()"> <script type="text/javascript"> /* <![CDATA[ */ function startAdPage(){ setInterval("changeAd()", 5000); setInterval("startCountdown()",1000); } pix = new Array(); pix[0] = "pictures/cvb2.gif"; pix[1] = "pictures/cvb3.gif"; pix[2] = "pictures/cvb1.gif"; var x = 0; function changeAd(){ document.images.pic.src = pix[x]; x = x + 1; if (x > (pix.length-1)) {x = 0} } var messages = new Array( "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1", "0" ); var count = 1; function startCountdown() { document.getElementById("timer").value = messages[count]; if(count < messages.length -1){ count++; } else { count = 0; location.assign ("CVR2.html"); } } /* ]]> */ </script> <table> <tr> <td><form><img name="pic" src="pictures/cvb1.gif"></form></td> <td><p>Advertisement</p><p>The Central Vally Realtors home page will be displayed in <form><input type="text" id="timer" value="15" /></form> seconds.</p><p><a href="CVR2.html">Skip Advertisement</a></p></td> </tr> </table> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM