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

需求:实现以下的功能

1.有三个小方块,分别用来当前输入的错误数量、打字的时间和当前的正确率。

2.下方是用来显示测试句子的容器。

3.最后是输入框

具体思路:

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

具体代码如下:

Html部分

打字测试

打字测试

  • 错误

    0

  • 时间

    60s

  • 当前准确率%

    100

  • 打字速度

    30个/分

点击下放文本输入框开始打字!!!

οninput="processCurrentText()"

οnfοcus="startGame()">

重新开始

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";

}

测试效果图:

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

html打字练习测试代码,JS实现简单打字测试相关推荐

  1. html选项卡切换代码,js实现简单的可切换选项卡效果

    本文实例讲述了js实现简单的可切换选项卡效果的方法.分享给大家供大家参考.具体如下: 如图,最简单的纯粹的选项卡 第一步,当然是先写html代码和css样式 无标题文档 body,ul,li{marg ...

  2. html放大镜效果代码,js实现简单放大镜效果

    用js实现简单放大镜效果,供大家参考,具体内容如下 此处放大镜实现的效果就是当鼠标放置在图片上会有半透明遮罩,图片的一个区域就会被放大,然后展示在右边.当鼠标移动时右边的大图片也会局部移动.这里的放大 ...

  3. C语言编一个金山打字通小游戏,js实现金山打字通小游戏

    本文实例为大家分享了js实现金山打字通小游戏的具体代码,供大家参考,具体内容如下 字母匀速随机下落,键盘按下对应字母按键,字母消失重新生成新字母,新字母可帮助回调一部分初始高度 效果 1.页面内容 列 ...

  4. 光流法测试代码_高效的企业测试-工作流和代码质量(4/6)

    光流法测试代码 本文的这一部分将讨论在开发过程中拥有有效工作流程的影响,以及适当的测试代码质量如何使我们能够创建可维护的测试,尤其是对于复杂项目. 开发工作流程和管道 编程是一项流程活动,我们开发人员 ...

  5. 网卡性能怎么测试软件,怎么用简单命令测试网络性能

    现如今,网络的使用已经十分普遍,同时也会有各种各样的局域网知识出现.比如,怎么用简单命令测试网络性能.如何才能知道线路质量的好坏呢?通过以下几个网络测试命令,可以有助于更好地使用和维护网络.学习啦小编 ...

  6. 简单html倒计时器代码,js倒计时简单实现代码

    倒计时: 1.设置一个有效的结束日期 2.计算剩余时间 3.将时间转换成可用的格式 4.输出时钟数据作为一个可重用的对象 5.在页面上显示时钟,并在它到达0时停止 html 天 时 分 秒 js代码 ...

  7. html js注册页面代码,JS一个简单的注册页面实例

    // $(function(){ // $("input[name='uname']").blur(function(){ // var unamestr = $(this).va ...

  8. Linux打字游戏程序代码,c语言shell打字游戏.pdf

    一.实验说明 1. 环境登录 无需密码自动登录 ,系统用户名shiyanlou ,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境 ,实验中 用到桌面上的程序 ...

  9. 使用javascript的网速测试代码

    使用javascript的网速测试代码: Javascript 最简单检测网速的方法.网速很慢,但又得打开 Flash 是一件很痛苦的事情,特别是 Silverlight 来临之际,这个技术可能有点用 ...

  10. 十五、Python第十五课——测试代码

    (请先看这篇文章:https://blog.csdn.net/GenuineMonster/article/details/104495419) 也许你听过软件测试?编写函数或类时,可以为其编写对应的 ...

最新文章

  1. R语言使用Repeat函数多次执行代码块内的语句,实现循环执行任务的功能:repeat没有提供任何检查条件,所以编码者必须给出退出重复循环的条件(一般使用if和break)
  2. python使用字典格式化字符串-Python中将(字典,列表等)变量格式化输出
  3. 【Linux】一步一步学Linux——tail命令(42)
  4. Silverlight 布局控件
  5. [转载]android一些、面试题
  6. hbase 集群(完全分布式)方式安装
  7. MATLAB人脸识别
  8. Java PdfBox 提取指定PDF页面图片
  9. 【DL学习笔记06】深度学习入门——基于Python的理论与实现(ch07: 卷积神经网络 CNN)
  10. 如何在 Linux 上最好地设置命令别名
  11. 小程序与微信会员卡打通教程
  12. [1] DevOps 自动化运维工具Chef----入门
  13. Service com.android.exchange.ExchangeService has leaked ServiceConnection
  14. 网路学员面试常见问题:
  15. 飞冰 前端开发的一些坑
  16. 宣州谢脁楼饯别校书叔云
  17. 什么是Web?Web Service、Web API傻傻分不清楚?
  18. 机器学习:二分类到多分类-ovr,ovo,mvm,sofmax
  19. 外汇券商TFS-ICAP因误导客户并使用虚假报告被FCA处罚340万英镑
  20. Flask前后端分离02

热门文章

  1. 如何安装JAVASE平台
  2. 《软件工程》课程:期末复习提纲(超详细课本内容)
  3. 语音合成(TTS)应用方案一二三
  4. 基于SSM的电脑商城
  5. 理解证券行业“行业分类
  6. qml textarea出现滚动条
  7. 程序设计导引及在线实践--读书笔记一
  8. 在MT4上使用双线MACD指标源码
  9. 【UI】NGUI和UGUI
  10. 2022年武汉科技大学成人高等学历教育招生简章--学历提升、高起专、专升本