JS实现简单打字测试


Posted in Javascript onJune 24, 2020

本文实例为大家分享了JS实现简单打字测试的具体代码,供大家参考,具体内容如下

需求:实现以下的功能

JS实现简单打字测试

1.有三个小方块,分别用来当前输入的错误数量、打字的时间和当前的正确率。
2.下方是用来显示测试句子的容器。
3.最后是输入框

具体思路:

点击输入文本区域时,开始测试,会根据用户输入来统计当前的错误数和正确率,时间会减少。当输完整段句子时,会自动更新下一段句子。当时间为0时,游戏结束,文本框不能再输入,然后会统计打字速度。

具体代码如下:

Html部分

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <link rel="stylesheet" href="./index.css" >
 <title>打字测试</title>
</head>
 
<body>
 <div class="type_content">
 <h3>打字测试</h3>
 <ul class="type_box">
  <li class="error_box">
  <p class="error">错误</p>
  <p class="error_text">0</p>
  </li>
  <li class="time_box">
  <p style="margin: 10px;">时间</p>
  <p class="time_text">60s</p>
  </li>
  <li class="currcorrect_box">
  <p style="margin: 10px;">当前准确率%</p>
  <p class="currcorrect_text">100</p>
  </li>
  <li class="type_speed">
  <p style="margin: 10px;">打字速度</p>
  <p class="type_speed_text">30个/分</p>
  </li>
 </ul>
 <div class="text_box">点击下放文本输入框开始打字!!!</div>
 <div class="text_area">
 <textarea name="" id="textarea_box" placeholder="start typing here..."
 oninput="processCurrentText()"
 onfocus="startGame()"> </textarea>
 </div>
 <button class="restart" onclick="resetValues()">重新开始</button>
 </div>
 <script src="./index.js"></script>
</body>
 
</html>

CSS部分:

*{
 margin: 0;
 padding: 0;
}
.type_content{
 width: 60%;
 /* height: 440px; */
 border: 1px solid #ccccff;
 max-width: 600px;
 margin: 50px auto;
 border-radius: 8px;
 position: relative;
 min-width: 500px;
}
.type_content h3{
 text-align: center;
 margin: 10px 0px;
}
.type_box{
 list-style: none;
 width: 90%;
 height: 100px;
 /* border: 1px solid black; */
 margin: 0 auto;
 margin-bottom: 10px;
 display: flex;
 align-items: center;
 justify-content: space-around;
}
.type_box li{
 width: 88px;
 height: 88px;
 /* border: 1px solid black; */
 text-align: center;
 font-size: 16px;
 border-radius: 8px;
 /* color: #fff; */
}
.error_box{
 background-color: #ccffcc;
 color: red;
}
.time_box{
 background-color: #66ffff;
 color: #000033;
}
.currcorrect_box{
 background-color: #FFC125;
 color: #fff;
}
.type_speed{
 background-color: #FF4040;
 color: #fff;
 display: none;
}
.final_correct{
 background-color: #E3E3E3;
 color: #555555;
 display: none;
}
.error{
 margin: 10px;
}
.text_box{
 width: 80%;
 /* height: 50px; */
 margin: 20px auto 40px auto;
 /* border: 1px solid black; */
 background-color: #D1EEEE;
 color: #000;
 border-radius: 6px;
 /* text-align: center; */
 line-height: 40px;
 padding: 0px 5px;
 /* box-sizing: border-box; */
}
.text_area{
 width: 80%;
 height: 100px;
 margin: 0px auto;
 padding-bottom: 50px;
 margin-bottom: 30px;
 
}
#textarea_box{
 resize:none;
 width: 100%;
 height: 100%;
 padding: 6px 10px;
 font-size: 16px;
 border-radius: 6px;
 outline: none;
 border: none;
 border: 2px solid #eee;
} 
.incorrect_char { 
 color: red; 
 text-decoration: underline; 
 } 
 
 .correct_char { 
 color: #9B30FF; 
 }
 .restart{
 width: 120px;
 height: 40px;
 display: none;
 border: none;
 outline: none;
 cursor: pointer;
 color: #fff;
 background-color: #9900ff;
 border-radius: 6px;
 position: absolute;
 bottom: 10px;
 left: 50%;
 margin-left: -60px;
 
}

JS部分:

//定义测试时间
var testTime = 60;
//定义测试的句子
var testSentence = [
 "Push yourself, because no one else is going to do it for you.",
 "Failure is the condiment that gives success its flavor.",
 // "Wake up with determination. Go to bed with satisfaction.",
 // "It's going to be hard, but hard does not mean impossible.",
 // "Learning never exhausts the mind.",
 // "The only way to do great work is to love what you do."
]
//定义dom
//1.错误dom
var error_text = document.querySelector('.error_text');
//2.时间dom
var time_text = document.querySelector('.time_text');
//3.当前正确率
var currcorrect_text = document.querySelector('.currcorrect_text');
//4.打字速度
var type_speed_text = document.querySelector('.type_speed_text');
 
//打字速度父dom
var type_speed = document.querySelector('.type_speed');
 
 
//句子dom
var text_box = document.querySelector('.text_box');
//输入
var textarea_box = document.querySelector('#textarea_box');
//按钮
var restart = document.querySelector('.restart')
var timeLeft = testTime;
//当前句子
var currentSentence = "";
//默认开始是索引为0
var startIndex = 0;
//错误统计
var errors = 0;
var characterTyped = 0;
//总错误
var total_errors = 0; 
var timer = null;
var timeElapsed = 0; //已用时间
var accuracy = 0;//正确个数
//更新渲染句子
function updateSentence(){
 text_box.textContent = null;
 currentSentence = testSentence[startIndex];
 //句子拆分
 var newChar = currentSentence.split('');
 for(var i = 0; i < newChar.length; i++){
 var charSpan = document.createElement('span');
 charSpan.innerText = newChar[i];
 text_box.appendChild(charSpan)
 }
 if(startIndex < testSentence.length - 1){
 startIndex++;
 }else{
 startIndex = 0
 }
}
//输入当前正确的句子
function processCurrentText(){
 curr_input = textarea_box.value;
 curr_input_array = curr_input.split('');
 // console.log(curr_input_array);
 characterTyped++;
 errors = 0;
 quoteSpanArray = text_box.querySelectorAll('span');
 // console.log(quoteSpanArray);
 quoteSpanArray.forEach((char,index)=>{
 var typeChar = curr_input_array[index];
 //当前没有输入
 if(typeChar == null){
  char.classList.remove('correct_char'); 
  char.classList.remove('incorrect_char'); 
 }else if(typeChar === char.innerText){
  //正确的输入
  char.classList.add('correct_char'); 
  char.classList.remove('incorrect_char'); 
 }else{
  //不正确的输入
  char.classList.add('incorrect_char'); 
  char.classList.remove('correct_char');
  errors++; 
 }
 })
 error_text.textContent = total_errors + errors; 
 console.log(characterTyped)
 console.log(error_text.textContent)
 var correctCharacters = (characterTyped - (total_errors + errors)); 
 var accuracyVal = ((correctCharacters / characterTyped) * 100); 
 console.log(accuracyVal)
 currcorrect_text.textContent = Math.round(accuracyVal); 
 //输入结束
 if(curr_input.length == currentSentence.length){
 updateSentence(); 
 total_errors += errors; 
 textarea_box.value = ""
 }
}
//开始游戏
function startGame(){
 resetValues(); 
 updateSentence(); 
 
 // clear old and start a new timer 
 clearInterval(timer); 
 timer = setInterval(updateTimer, 1000); 
}
//重新开始
function resetValues(){
 timeLeft = testTime;
 timeElapsed = 0;
 errors = 0;
 total_errors = 0;
 accuracy = 0;
 characterTyped = 0;
 startIndex = 0;
 textarea_box.disabled = false;
 textarea_box.value = "";
 text_box.textContent = 'Click on the area below to start the game.'; 
 currcorrect_text.textContent = 100;
 time_text.textContent = timeLeft + 's';
 type_speed.style.display = 'none';
 
}
//更新时间
function updateTimer() { 
 if (timeLeft > 0) { 
 // decrease the current time left 
 timeLeft--; 
 
 // increase the time elapsed 
 timeElapsed++; 
 
 // update the timer text 
 time_text.textContent = timeLeft + "s"; 
 } 
 else { 
 // finish the game 
 finishGame(); 
 } 
 } 
 //游戏结束
 function finishGame() { 
 // stop the timer 
 clearInterval(timer); 
 
 // disable the input area 
 textarea_box.disabled = true; 
 
 // show finishing text 
 text_box.textContent = "可点击下方按钮重新开始!!!"; 
 
 // display restart button 
 restart.style.display = "block"; 
 
 // calculate cpm and wpm 
 console.log(characterTyped,timeElapsed)
 cpm = Math.round(((characterTyped / timeElapsed) * 60)); 
 
 // update cpm and wpm text 
 
 type_speed_text.textContent = cpm + '个/分'; 
 
 // display the cpm and wpm 
 type_speed.style.display = "block"; 
 
}

测试效果图:

JS实现简单打字测试

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
jquery插件珍藏(图片局部放大/信息提示框)
Jan 08 Javascript
谈谈AngularJs中的隐藏和显示
Dec 09 Javascript
详解AngularJS controller调用factory
May 19 Javascript
20多个小事例带你重温ES10新特性(小结)
Sep 29 Javascript
学前端,css与javascript重难点浅析
Jun 11 Javascript
js实现小球在页面规定的区域运动
Jun 16 Javascript
vue实现在进行增删改操作后刷新页面
Aug 05 Javascript
JavaScript语句错误throw、try及catch实例解析
Aug 18 Javascript
JS实现简单贪吃蛇小游戏
Oct 28 Javascript
详解JavaScript 中的批处理和缓存
Nov 19 Javascript
微信小程序向Java后台传输参数的方法实现
Dec 10 Javascript
JS前端canvas交互实现拖拽旋转及缩放示例
Aug 05 Javascript
微信小程序实现多选框功能的实例代码
Jun 24 #Javascript
JS实现电脑虚拟键盘的操作
Jun 24 #Javascript
基于PHP pthreads实现多线程代码实例
Jun 24 #Javascript
js实现html滑动图片拼图验证
Jun 24 #Javascript
微信小程序的引导页实现代码
Jun 24 #Javascript
微信小程序仿抖音短视频切换效果的实例代码
Jun 24 #Javascript
微信小程序swiper组件实现抖音翻页切换视频功能的实例代码
Jun 24 #Javascript
You might like
PHP curl 抓取AJAX异步内容示例
2014/09/09 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
2020/05/15 PHP
CSS心形加载的动画源码的实现
2021/03/09 HTML / CSS
一款jquery特效编写的大度宽屏焦点图切换特效的实例代码
2013/08/05 Javascript
Nodejs+express+html5 实现拖拽上传
2014/08/08 NodeJs
在Javascript中处理数组之toSource()方法的使用
2015/06/09 Javascript
jQuery实现的背景动态变化导航菜单效果
2015/08/24 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
AngularJS下$http服务Post方法传递json参数的实例
2018/03/29 Javascript
详解关于webpack多入口热加载很慢的原因
2019/04/24 Javascript
浅谈redux, koa, express 中间件实现对比解析
2019/05/23 Javascript
webpack3.0升级4.0的方法步骤
2020/04/02 Javascript
详解使用mocha对webpack打包的项目进行&quot;冒烟测试&quot;的大致流程
2020/04/27 Javascript
判断网页编码的方法python版
2016/08/12 Python
python线程池threadpool实现篇
2018/04/27 Python
Django rest framework实现分页的示例
2018/05/24 Python
机器学习之KNN算法原理及Python实现方法详解
2018/07/09 Python
Python实现矩阵相乘的三种方法小结
2018/07/26 Python
对Python实现简单的API接口实例讲解
2018/12/10 Python
python SQLAlchemy的Mapping与Declarative详解
2019/07/04 Python
python实现矩阵和array数组之间的转换
2019/11/29 Python
详解pandas中iloc, loc和ix的区别和联系
2020/03/09 Python
Python用SSH连接到网络设备
2021/02/18 Python
英国时尚配饰、珠宝和服装网站:KJ Beckett
2020/01/23 全球购物
幼儿师范毕业生自荐信
2013/11/09 职场文书
期末自我鉴定
2014/01/23 职场文书
《维生素c的故事》教学反思
2014/02/18 职场文书
公务员培的训心得体会
2014/09/01 职场文书
三严三实学习心得体会
2014/10/13 职场文书
工作业绩不及格检讨书
2014/10/28 职场文书
深入理解java.lang.String类的不可变性
2021/06/27 Java/Android
oracle索引总结
2021/09/25 Oracle
如何用vue实现网页截图你知道吗
2021/11/17 Vue.js
Spring Cloud Netflix 套件中的负载均衡组件 Ribbon
2022/04/13 Java/Android