1. 程式人生 > >javascript基礎:window物件定時器

javascript基礎:window物件定時器

程式碼:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script>
	function move(flag)
	{
		if(flag == 1)
		{
			//滾動視窗內容到 偏移位置
			window.scrollBy(20,0);  

			//window定時器,只會執行一次
			timeout = setTimeout("move(1)",500);  
		}
		else
			if(flag == 2)
			{
				if(i>450)  
					i = 0;

				//滾動視窗內容到 絕對位置
				window.scroll(i,0);
				i += 50;
			}
	}	

	function ss()
	{
		//和setTimeout不同,這個函式會週期執行,單位也是毫秒
		interval=setInterval("move(2)",500);
	}

  </script>
 </head>
 <body>
	<script>
		window.defaultStatus = "這個屬性現在大部分瀏覽器都不支援";
		window.status = "只有opera還支援";

		var timeout;
		var interval;
		var i = 0;

	</script>

	<form>
		<input type="button" value="啟動Time" onclick="move(1);">
		<input type="button" value="停止Time" onclick="clearTimeout(timeout);">
        
		<input type="button" value="啟動Interval" onclick="ss();">
		<input type="button" value="停止Interval" onclick="clearInterval(interval);">
	</form>

	<pre>
	 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	</pre>
	
  
 </body>
</html>