JS控制有概率的隨機,實現今天喫什麼

解決千古難題,直接上代碼。

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>今天喫什麼?</title>
		<script type="text/javascript">
		function start(){
				var select =  new Array("羊肉粉",	//0	
										"米皮",		//1	
										"炒飯",		//2	
										"快餐",		//3	
										"炒菜",		//4	
										"酸辣粉/重慶小面",//5 
										"黃燜雞",	//6一個月喫一次,概率爲 1/30
										"去外面喫",	//7一個月喫一次,概率爲 1/30
										"雞公煲",	//8兩個星期喫一次,概率爲 1/14
										"牛牛蓋飯"	//9	概率爲 1/價格 == 1/11
				);
				var a;
				if(Math.round(Math.random()*900)==1){
					if(Math.round()>0.5){
						a = 6;
					}else{
						a = 7;
					}
				}else if(Math.round(Math.random()*14)==1){
					a = 8;
				}else if(Math.round(Math.random()*11)==1){
					a = 9;
				}else{
					a=Math.round(Math.random()*4);	
				}
				document.getElementById("t1").innerHTML=select[a];
				id=window.setTimeout("start()",75);
				document.getElementById("start").disabled="disabled";
				document.getElementById("stop").disabled="";
			}
			function stop(){
			
				window.clearTimeout(id);

				document.getElementById("stop").disabled="disabled";
				document.getElementById("start").disabled="";
			}
		</script>
		<style type="text/css">
			th {
				
				font-size:150px;
				font-style: normal;
				line-height: normal;
				font-weight: bold;
				font-variant: normal;
			}
			table{
				margin-top:200px;
				margin-bottom:200px;
			}
		</style>
	</head>
	<body>
		<button id="start" onclick="start()" style="width:80px;height:40px;">Start</button>
		<button id="stop" onclick="stop()" style="width:80px;height:40px;" disabled="disabled">Stop</button>
		<table border="0" align="center">
			<tr>
				<th id="t1">
					今天喫什麼?
				</th>
			</tr>
		</table>
	</body>
</html>

如果有時間還可以加圖片美化一下界面什麼的,比如加個大輪盤什麼的,現在就先暫時這樣了。
演示地址:今天喫什麼?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章