Home > Web Front-end > JS Tutorial > Use JavaScript to simulate typing games!

Use JavaScript to simulate typing games!

藏色散人
Release: 2022-08-07 10:34:42
forward
2080 people have browsed it

I have nothing to do today, and I want to try a simulated typing game. The following is the rendering, which will record and judge typing. If you add the setInterval function to the periphery, you can test the typing speed.

html

<div class="wrapper">
  <div id="text">A</div>
  <input type="text" id="ipt">
</div>
Copy after login

css

body,
html {
  width: 100%;
  height: 100%;
 }

 .wrapper {
  width: 400px;
  margin: 20px auto;
 }

 div {
  font-size: 100px;
  font-weight: 900;
  text-align: center;

 }
 input{
  width: 400px;
  margin: 20px auto;
 }
Copy after login

js

var str = &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;
var text = document.getElementById(&#39;text&#39;) 
var ipt = document.getElementById(&#39;ipt&#39;)
var res;
var newRes;
var random = Math.round(Math.random() * 25);
var count = 0
 window.onkeyup = function (e) {
  var random = Math.round(Math.random() * 25);
  newRes = str[random]
  res = text.innerHTML
  text.innerHTML = newRes
  if(e.keyCode == res.charCodeAt(0)){
   count++;
   ipt.value = &#39;&#39;;
  }else{
   alert(&#39;game over&#39; + &#39;   &#39; +&#39;您的得分是&#39;+ &#39;:&#39; + count)
   count = 0;
   ipt.value = &#39;&#39;;
  }

 }
Copy after login

Related recommendations: [JavaScript Video Tutorial

The above is the detailed content of Use JavaScript to simulate typing games!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template