Javascript implementation of a random call function

  • 2020-03-30 03:46:21
  • OfStack

This is actually an exercise for JS random Numbers. First, write the names of all the people in an array, and then the values in the array will be displayed in the region quickly. When you press stop, the scrolling will stop to achieve random effect.


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>JS Write a random roll call  -  Joan Taiwan blog </title>
	<script type="text/javascript">
	var isRun=true;
	var a = [" Zhang SAN "," Li si "," Cathy "," Zhao six "];	
	var a2 = new Array();
	function action(str){
		var s = document.getElementById("bt").value;
		if(s==" start "){
			isRun=true;
			run();
			document.getElementById("bt").value=" The end of the ";
		}else{
			isRun=false;
			document.getElementById("bt").value=" start ";
		}
	}
	function run(){
		var i = Math.floor(Math.random() * a.length+ 1)-1;
		document.getElementById("show").innerHTML=a[i];
		if(isRun==false){
			var b =true;
			for(var j in a2){
				if(a2[j]==i){
					b=false;
				}
			}
			if(b){
			 	a2[a2.length]=i;
				return;
			}
		}
		setTimeout("run()",10);
	}
</script>

</head>

<body>
	<div style="text-align:center; margin-top:100px;width:100%;">
		<div id="show" style="margin:auto;font-size:50px;width:100px;height:50px; background:#FFEEFF"></div>
		<div style="margin-top:20px;">
			<input id="bt" type="button" onclick="action()" value=" start "/>
		</div>
	</div>
</body>
</html>


Related articles: