Demo 演示地址:http://janycode.github.io/random/index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body onload="getFood()"> 世界难题"中午吃什么?"解决方案:<br><br> <center> <span id="food"></span> <hr style="width: 450px;" /> <input type="button" id="button" style="font-size: 30px;" value="开始" onclick="run()" /> </center> </body> <script type="text/javascript"> function getFood() { var arr = ['盖浇饭', '油泼面', '焖面', '凉皮', '兰州拉面', '炒面', '热干面']; var index = Math.floor(Math.random() * arr.length); var code = arr[index]; var spanNode = document.getElementById("food"); spanNode.innerText = code; spanNode.style.fontSize = "99px"; spanNode.style.color = "red"; spanNode.style.backgroundColor = "lightyellow"; spanNode.style.fontFamily = "楷体"; }
var id; var cnt = 0; function run() { cnt++; if (cnt % 2 == 0) { document.getElementById("button").value = "开始"; clearInterval(id); } else { document.getElementById("button").value = "暂停"; id = setInterval("getFood()", 50); } } </script> </html>
|