<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>HTML5 Timer</title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> </head>
<body>
<div style="margin: 20px auto; text-align: center;">
<div style="padding: 50px;">
<input type="button" id="btnstart" value="start" />
</div>
<canvas id="canvas" width="200" height="200" style="border: 1px solid #000;">Chrome,IE9,IE10,Firefox。</canvas>
<h2>Please Choose the correct answer:</h2>
<h3>1: 1+1=?</h3> <input type="radio" name="answer" value="1" />1
<input type="radio" name="answer" value="2" />2
<input type="radio" name="answer" value="3" />3
<input type="radio" name="answer" value="4" />4<br />
<input type="button" id="btnSubmit" value="submit" />
<div style="padding: 50px;">
<input type="button" id="btnStop" value="Stop" />
</div>
</div> <script type="text/javascript"> $('<audio id="auSound"><source src="sound/wrong_answer_buzzer.mp3" type="audio/mpeg"></audio><audio id="auCorrect"><source src="sound/correct_answer_bell_ring.mp3" type="audio/mpeg"></audio>').appendTo('body'); var canvas = document.getElementById('canvas'); var timerId; var ctx = canvas.getContext('2d');
if (ctx) {
var frameRate = 60;
function canvObject() {
this.x = 0;
this.y = 0;
this.rotation = 0;
this.borderWidth = 2;
this.borderColor = '#000000';
this.fill = false;
this.fillColor = '#ff0000';
this.update = function () {
if (!this.ctx) throw new Error('not canvas');
var ctx = this.ctx
ctx.save();
ctx.lineWidth = this.borderWidth;
ctx.strokeStyle = this.borderColor;
ctx.fillStyle = this.fillColor;
ctx.translate(this.x, this.y);
if (this.rotation) ctx.rotate(this.rotation * Math.PI / 180);
if (this.draw) this.draw(ctx);
if (this.fill) ctx.fill();
ctx.stroke();
ctx.restore();
}
}; function Line() { }; Line.prototype = new canvObject();
Line.prototype.fill = false;
Line.prototype.start = [0, 0];
Line.prototype.end = [5, 5];
Line.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.moveTo.apply(ctx, this.start);
ctx.lineTo.apply(ctx, this.end);
ctx.closePath();
};
function Circle() { };
Circle.prototype = new canvObject();
Circle.prototype.draw = function (ctx) {
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, 2 * Math.PI, true);
ctx.closePath();
};
var circle = new Circle();
circle.ctx = ctx;
circle.x = 100;
circle.y = 100;
circle.radius = 90;
circle.fill = true;
circle.borderWidth = 6;
circle.fillColor = '#ffffff'; var seconds = new Line();
seconds.ctx = ctx;
seconds.x = 100;
seconds.y = 100;
seconds.borderColor = "#ff0000";
seconds.borderWidth = 4;
seconds.rotation = 0;
seconds.start = [0, 20];
seconds.end = [0, -80];
var center = new Circle();
center.ctx = ctx;
center.x = 100;
center.y = 100;
center.radius = 5;
center.fill = true;
center.borderColor = 'orange'; for (var i = 0, ls = [], cache; i < 12; i++) {
cache = ls[i] = new Line();
cache.ctx = ctx;
cache.x = 100;
cache.y = 100;
cache.borderColor = "orange";
cache.borderWidth = 2;
cache.rotation = i * 36;
cache.start = [0, -70];
cache.end = [0, -80];
} //clear canvas
ctx.clearRect(0, 0, 200, 200);
ctx.fillRect(0, 0, 200, 200);
circle.update();
for (var i = 0; cache = ls[i++];) cache.update();
seconds.update(); center.update(); } else {
alert('error');
} $("#btnstart").live("click", function () {
var number = 0;
$("input[name=answer]").attr("disabled", false);
clearInterval(timerId);
timerId = setInterval(function () {
if (number == 10) {
clearInterval(timerId);
$('#auSound')[0].play();
$("input[name=answer]").attr("disabled", true);
}
ctx.clearRect(0, 0, 200, 200);
ctx.fillRect(0, 0, 200, 200);
circle.update();
for (var i = 0; cache = ls[i++];) cache.update();
seconds.rotation = (number++) * 6 * 6;
seconds.update();
center.update();
}, 1000);
}) $("#btnStop").live("click", function () {
clearInterval(timerId);
}); $("#btnSubmit").live("click", function () {
clearInterval(timerId);
if ($(":radio:checked").val() == "2") {
$('#auCorrect')[0].play();
alert("correct");
} else {
$('#auSound')[0].play();
alert("wrong"); }
});
</script>
</body>
</html>

这个计时器如果超过十秒将播放错误声音,如果回答正确,将播放正确声音

这是效果图:

简单的计时器 (倒计时)--html Demo的更多相关文章

  1. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...

  2. 如何使用AEditor制作一个简单的H5交互页demo

    转载自:http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/ 本教程演示如何使用AEditor制作一个 ...

  3. 一个简单的MariaDB认证插件demo

    代码地址如下:http://www.demodashi.com/demo/13076.html 一.前言 众所周知(其实可能很多人不知道)MariaDB支持插件认证.在MariaDB中新建用户,常见的 ...

  4. unity3d之简单的时钟倒计时demo

    输入结束时间,开始倒计时,时间差不超过一天,附上代码:(关于个位数显示,加个判断如果小于10 显示的字符串加上0) using System.Collections; using System.Col ...

  5. 也谈SSO,一个简单实用的单点登录Demo

    关于SSO(单点登录),百度百科解释如下 : “SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要 ...

  6. 【Time系列三】简单的计时器(秒表)

    之前在 "for与while的洪荒之力" 中介绍到计时器,不过那样弄感觉好麻烦啊, 碰巧昨天学Java的时候,讲到求余可以用来求时间 ! for与while链接: http://w ...

  7. 一个简单的IM系统(Demo附源码)-- ESFramework 4.0 快速上手(08)

    前面的文章已经介绍完了基于ESFramework/ESPlus进行二次开发的所有要点,现在,我们可以开始小试牛刀了. 本文将介绍使用ESFramework的Rapid引擎开发的两个最简单的Demo,E ...

  8. MySQL两个最简单的delimiter的使用demo

    今天复习MySQL,使用的工具是Navicat for MySQL,写了两个简单的delimiter的demo. 第一个,获取当前时间的年月日时分秒格式的时间: delimiter $$ drop f ...

  9. 毫秒倒计时小Demo

    Demo截图: Demo:Demo 上代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

随机推荐

  1. 批量群发,营销必备!Python代码实现自动发送邮件!

    在运维开发中,使用 Python 发送邮件是一个非常常见的应用场景.今天一起来探讨一下,GitHub 的大牛门是如何使用 Python 封装发送邮件代码的. 一般发邮件方法 SMTP是发送邮件的协议, ...

  2. C#基础知识1-深入理解值类型和引用类型

    C#值类型和引用类型这个概念在刚学习的时候应该就知道了.但是我们并没有深入的去理解它.越是基础知识其实才是最有用的.对代码的优化,代码质量的提升都有帮助.通过整理本文章,对很多知识也起到了巩固的作用吧 ...

  3. ASP.NET Core部署系列一:发布到IIS上

    前言: 当构建一个ASP.NET Core应用程序并且计划将其运行在IIS中时,你会发现Core应用程序和之前版本的ASP.NET程序在IIS中的运行方式是完全不一样的.与ASP.NET时代不同,AS ...

  4. MAC 软件提示已损坏,需要移到废纸篓的解决方法

    解决方法一: 允许任何来源的应用.在系统偏好设置里,打开“安全性和隐私”,将“允许从以下位置下载的应用程序”设置为“任何来源“.当然,这个设置已经无法在Mac OS Sierra上完成了. 在Mac ...

  5. 如何使用块更改跟踪文件估算RMAN增量备份大小 (Doc ID 1938079.1)

    How to estimate RMAN incremental backup size using block change tracking file (Doc ID 1938079.1) APP ...

  6. Druid-代码段-5-1

    所属文章:池化技术(一)Druid是如何管理数据库连接的? 本代码段对应主流程5,连接的回收: //DruidPooledConnection类的close方法 @Override public vo ...

  7. go语言设计模式之Chain Of Responsibility(责任链)

    ChainOfResponsibility.go package ChainOfResponsibility import ( "fmt" "io" " ...

  8. linux的路由功能实现

    参考URL: https://blog.csdn.net/chengqiuming/article/details/80140768 一,启用Linux的路由转发功能. 二,新建veth pair 三 ...

  9. Deepin nginx lumen配置

    正常安装 sudo apt install nginxsudo apt install php-fpm 启动后将 /etc/nginx/sites-enabled/default 配置文件 copy一 ...

  10. All 432 functions were compiled because no usable IPDB/IOBJ from previous compilation was found

    解决方法: 将: 链接器——>优化——>链接时间代码生成——>使用快速链接时间代码生成(/LTCG:incremental). 改为: 链接器——>优化——>链接时间代码 ...