5-4 �w���{���X������

­Y­n¹w¬ùµ{¦¡½X¦b¬Y¤@¬q®É¶¡«á¤~³Q°õ¦æ¡A¥i¥Î setTimeout() ªº¤º«Ø¨ç¼Æ¡A¨Ò¦p¦b¤U¦Cªº½d¨Ò¤¤¡A·í§AÂI¿ï¡u¥á¤âºh¼u¡v«á¡A¤T¬íÄÁ¤§«á¤~·|¥X²{¤@­Óĵ§iµøµ¡¡uÅF¡I¡I¡I¡v¡G

Example¡]setTimeout01.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]setTimeout01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¹w¬ù©M¨ú®øµ{¦¡½Xªº°õ¦æ¡G¹w¬ù¤@¦¸</h2>
<hr>

<script>
function clickCallback(){
	document.theForm.theButton.value="¤T¬íÄÁ«á¤ÞÃz...";
	setTimeout("alert('ÅF¡I¡I¡I'); document.theForm.theButton.value='¥á¤âºh¼u'", 3*1000);
}
</script>

<center>
<form name="theForm">
<input type="button" name=theButton value="¥á¤âºh¼u" onClick="clickCallback()">
</form>
</center>

<hr>
</body>
</html>

¥Ñ¤W­z½d¨Ò¥iª¾¡AsetTimeout() ªº®æ¦¡¦p¤U¡G

timer = setTimeout("JavaScriptªº©R¥O¦C", ®É¶¡ªø«×)
´«¥y¸Ü»¡¡A·í¹L¤F¡u®É¶¡ªø«×¡v©Ò«ü©wªº®É¶¡¡]¥H1/1000¬í¬°³æ¦ì¡^«á¡AÂsÄý¾¹´N·|¥h°õ¦æ¡u"JavaScriptªº©R¥O¦C"¡v¡C¦Ó©Ò¶Ç¦^ªº¿é¥X°Ñ¼Æ timer¡A¥i³Q¥Î¦b clearTimeout() ¥H«K¸Ñ°£¹w¬ùªºµ{¦¡½X¡]¸Ô¨£«á­z¡^¡C

¦pªG§Ú­Ì­n¤ÏÂЦa¦b©T©wªº®É¶¡¶¡®æ¨Ó°õ¦æ¬Y¬qµ{¦¡½X¡A´N¥²¶·±N setTimeout() ©ñ¦b¨ç¼Æ¸Ì­±¡A¥H«OÃÒ¦¹¨ç¼Æ¯à°÷¦b¯S©wªº®É¶¡¶¡¹j«á¡A¤ÏÂЦa©I¥s¦Û¤v¡A¶]°¨¿O´N¬O¤@­Ó³Ì²³æªº½d¨Ò¡A¦b¤U¦C½d¨Ò¤¤¡A§Ú­Ì¤@¤f®ð²£¥Í¤T­Ó¶]°¨¿O¡A¤À§OÅã¥Ü¦bªí³æªº¤å¦rÄæ¦ì¡B<div> ¼ÐÅÒ¤º¡B¥H¤Îµøµ¡ªºª¬ºA¦C¡A½d¨Ò¦p¤U¡G

Example¡]movingText01.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]movingText01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¶]°¨¿O</h2>
<hr>

<input type=text id=myText1 size=40>
<div id=myText2></div>

<script>
text="¨ºªe¯`ªºª÷¬h¡A¬O¤i¶§¤¤ªº·s®Q¡Fªi¥ú¸ÌªºÆv¼v¡A¦b§Úªº¤ßÀY¿ººy...";
text=text+"¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@¡@";	// ¥[¤@¨Ç¥þ§ÎªÅ¥Õ

function showMovingText(){
	var n = text.length;
	text = text.substr(1, n-1)+text.substr(0, 1);		// ±N²Ä¤@­Ó¦r·h¨ì³Ì«á­±
	document.getElementById("myText1").value = text;	// ³]©w¦Ü¤å¦rÄæ¦ì
	document.getElementById("myText2").innerHTML = text;	// ³]©w¦Ü <div> ¼ÐÅÒ¤º¤å
	window.status = text;					// ³]©w¦Üª¬ºA¦C
	setTimeout("showMovingText()", 400);			// 0.4 ¬í°{¤@¹M
}

showMovingText();
</script>

<hr>
</body>
</html>

¦b¤W­z½d¨Òªº showMovingText() ¨ç¼Æ¤¤¡A³Ì«á¤@¦C¬O

setTimeout("showMovingText()", 400);
·í¨ç¼Æ°õ¦æ¨ì¦¹¦C®É¡A¥i«OÃÒ¦b 0.4 ¬í«á¡A¦¹¨ç¼Æ¤S·|³Q©I¥s¤@¦¸¡A¨Ì¦¹Ãþ±À¡A¦]¦¹¶]°¨¿Oªº¤å¦r¡A·|¦b¶¡¹j 0.4 ¬í«á´N·|³Q§ó·s¤@¦¸¡C¥t¥~¡A¨C¦¸§ó·s®É¡A§Ú­Ì¨Ï¥Î substr() ¨Ó±N²Ä¤@­Ó¦r¤¸·h¨ì¦r¦êªº§ÀºÝ¡A¦p¦¹¤ÏÂзh²¾¡A´N¥i¥H¹F¨ì¶]°¨¿Oªº®ÄªG¡C

Hint
¦³Ãö©ó substr() ©M substring() ªº¨Ï¥Î¡A¦A´£¥Ü¤@¤U¡G¦pªG text = "§ÚÄ@¬O¤d¸U±ø¦¿ªe"¡A«h
  • text.substr(3,5)·|¶Ç¦^ "¤d¸U±ø¦¿ªe"¡]²Ä3­Ó¦r¤¸¶}©l¡A¨ú5­Ó¦r¤¸¡^
  • text.substring(3,5)·|¶Ç¦^ "¤d¸U"¡]²Ä3­Ó¦r¤¸¶}©l¡A²Ä4­Ó¦r¤¸µ²§ô¡^

Hint
¨Ï¥Î setTimeout() ¨Ó¤ÏÂЩI¥s¤@­Ó¨ç¼Æ¡A¨Ã¤£¥Nªí¬O¦¹¨ç¼Æªº»¼°j©I¥s¡A½Ðª`·N³o¨âªÌªº®t²§¡C

Hint
id ©M name Äݩʦb JavaScript ªº¥Îªk¦³¨Ç¤£¦P¡Gid ´N¹³¥þ°ìÅܼƤ@¼Ë¡A¥i¦b JavaScript ¤¤¥i¥Hª½±µ©I¥s¡A¦ý¬O¥²¶·¨Ï¥Î document.getElementById("id ¼ÐÅÒ­È") ¨Ó±o¨ì¹ïÀ³ªºª«¥ó¡F¦Ó name ´N¹³§½³¡ÅܼơA¥~³¡ª½±µ¬Ý¤£¨ì¡A¥²¶·¸g¥Ñ DOM ±q¤W©¹¤U§ä¡A¨Ò¦p document.theForm.theButton µ¥¡C

­Y­n¨ú®ø¹w¬ùªºµ{¦¡½X¡A¥i¥Î clearTimeout()¡A¨Ò¦p¡G

Example¡]setTimeout02.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]setTimeout02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¹w¬ùµ{¦¡½Xªº°õ¦æ¡G¹w¬ù»P¨ú®ø</h2>
<hr>

<script>
timeStep=0.1;	// ¨C 0.1 ¬íÅã¥Ü¤@¦¸®É¶¡
accumulated=0;	// ¥Ø«e²Ö­pªº®É¶¡
function showTime(){
	document.getElementById("theText").value=accumulated;
	accumulated+=timeStep;
	timer=setTimeout("showTime();", timeStep*1000);
}

function clickCallback(){
	switch(document.getElementById("theButton").value){
		case "¶}©l­p®É":
			document.getElementById("theButton").value="°±¤î­p®É";
			accumulated=0;					// ®É¶¡Âk¹s
			showTime();					// ¶}©l­p®É
			break;
		case "°±¤î­p®É":
			document.getElementById("theButton").value="¶}©l­p®É";
			clearTimeout(timer);				// °±¤î­p®É
			break;
		default:
			alert("Error!");
			break;
	}
}
</script>

<center>
<form>
<input type="button" id=theButton value="¶}©l­p®É" onClick="clickCallback()">
<input type="text" id=theText>
</form>
</center>

<hr>
</body>
</html>

¤W­z½d¨Ò¦³¨â­Ó­«ÂI¡G

  1. ­Y«ö¤U¡u¶}©l­p®É¡v¡A§Ú­Ì¨Ï¥Î¡utimer=setTimeout("showTime();", timeStep*1000);¡v¨Ó¹w¬ù showTime() ¦b¨C 0.1 ¬í°õ¦æ¤@¦¸¡C
  2. ­Y«ö¤U¡u¨ú®ø­p®É¡v¡A«h§Ú­Ì¨Ï¥Î¡uclearTimeout(timer);¡v¨Ó¨ú®ø¤w¸g¹w¬ùªºµ{¦¡½X¡C
¨Ï¥Î setTimeout() ©M clearTimeout()¡A§Ú­Ì¥i¥H¦³®Ä¦a¹ï¡u¨Æ¥óÅX°Ê¡vªº JavaScript ¶i¦æ²¼ä§¹¾ãªº®É¶¡±±¨î¡C

¥Ñ©ó¡u¤ÏÂаõ¦æ¤@¬qµ{¦¡½X¡v¬O«Ü­«­nªº¥\¯à¡A¦]¦¹°£¤F¨Ï¥Î setTimeout() ¤§¥~¡AJavaScript «áÄò¤S¤ä´©¥t¤@­Ó¨ç¼Æ setInterval()¡A¥i¥H¹F¨ì¬Û¦Pªº¥\¯à¡A¦ý¦b¨Ï¥Î¤W§ó¬°Â²¼ä¡CsetInterval() ªº®æ¦¡¦p¤U¡G

timer = setInterval("JavaScriptªº©R¥O¦C", ®É¶¡ªø«×)
´«¥y¸Ü»¡¡A¨C¹j¡u®É¶¡ªø«×¡v©Ò«ü©wªº®É¶¡¡]¥H1/1000¬í¬°³æ¦ì¡^«á¡AÂsÄý¾¹´N·|¥h°õ¦æ¡u"JavaScriptªº©R¥O¦C"¡v¡A¦Ó©Ò¶Ç¦^ªº¿é¥X°Ñ¼Æ timer ¥i³Q¥Î¦b clearInterval() ¥H«K¸Ñ°£¹w¬ùªºµ{¦¡½X¡]¸Ô¨£«á­z¡^¡C­º¥ý§Ú­Ì¬Ý¬Ý¥Ñ setInterval() ©Ò²£¥Íªº§Y®É§ó·s®É¶¡Åã¥Ü¡G

Example¡]setInterval01.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]setInterval01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¹w¬ùµ{¦¡½Xªº°õ¦æ¡G¨Ï¥Î setInterval()</h2>
<hr>

<script>
function showTime(){
	date=new Date();
	document.getElementById("theText").value=date.toLocaleString();
}
setInterval("showTime()", 1000);		//¨C¤@¬í§ó·s¤@¦¸
</script>

<center>
<form>
¥Ø«e®É¶¡¡G<input type="text" id=theText size=30>
</form>
</center>

<hr>
</body>
</html>

­Y­n°±¤î®É¶¡Åã¥Ü¡A¥i¥H¨Ï¥Î clearInterval()¡A½Ð¨£¤U¦C½d¨Ò¡G

Example¡]clearInterval01.htm¡^¡G

¨ä­ì©lÀɮפº®e¦p¤U¡G

­ì©lÀÉ¡]clearInterval01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>¹w¬ù»P°±¤îµ{¦¡½Xªº°õ¦æ¡G¨Ï¥Î setInterval() ©M clearInterval()</h2>
<hr>

<script>
function showTime(){
	date=new Date();
	document.getElementById("theText").value=date.toLocaleString();
}
clock=setInterval("showTime()", 1000);		//¨C¤@¬í§ó·s¤@¦¸
</script>

<center>
<form>
¥Ø«e®É¶¡¡G<input type="text" id=theText size=30>
<input type=button name=controlButton value="°±¤î®ÉÄÁ" onClick="clearInterval(clock)">
</form>
</center>

<hr>
</body>
</html>

¤W­z½d¨Ò¥u¬O¤@­Ó²³æ½d¨Ò¡A©Ò¥H§A¤@¥¹°±¤î®É¶¡Åã¥Ü¡A´N¨S¿ìªk¦A«ì´_¡A°£«D­«·s¸ü¤Jºô­¶¡C


JavaScript µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ