简单的计时器 (倒计时)--html Demo

<!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>

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

这是效果图:

时间: 2024-08-03 21:45:07

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

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

转载自:http://www.alloyteam.com/2015/06/h5-jiao-hu-ye-bian-ji-qi-aeditor-jie-shao/ 本教程演示如何使用AEditor制作一个简单的H5交互页demo: 交互页demo地址: 点击打开H5交互页demo AEditor访问地址: http://aeditor.alloyteam.com Step1:设置页面背景颜色 首先我们设置页面的背景颜色,右击舞台点击“设置背景”: 然后在背景颜色中填上色值rgb(38, 61, 10

简单的图片抓取demo

原文:简单的图片抓取demo 源代码下载地址:http://www.zuidaima.com/share/1568741405854720.htm 昨天看到同学在一张张右键图片,感觉好麻烦,今天上午就查了一下资料,弄了个图片抓取器. 用到jsoup和 io包,我放在压缩文件里了. 新手刚刚弄,可能会有考虑不好的地方,欢迎大家多多指教. 主要代码: 01 //遍历保存 02 Iterator<String> i = imgSrcSet.iterator(); 03             whi

nodejs 简单http 文件上传demo

// 这是一个简单的Node HTTP,能处理当前目录的文件 // 并能实现良种特殊的URL用于测试 // 用http://localhost:8000 或http://127.0.0.1:8000 连接这个服务器 // 首先,加载所有要用的模块 var http = require('http'); // HTTP服务器API var fs = require('fs'); // 文件系统API var server = new http.Server(); // 创建新的HTTP服务器 va

js简单时分秒倒计时

最近做浏览器界面倒计时,用js就实现,两种方式: 一:设置时长,进行倒计时.比如考试时间等等 代码如下: 1 <html> 2 <head> 3 <meta charset="UTF-8"> 4 <title>简单时长倒计时</title> 5 <SCRIPT type="text/javascript"> 6 var maxtime = 60 * 60; //一个小时,按秒计算,自己调整! 7

一个简单的MariaDB认证插件demo

代码地址如下:http://www.demodashi.com/demo/13076.html 一.前言 众所周知(其实可能很多人不知道)MariaDB支持插件认证.在MariaDB中新建用户,常见的语句是: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 这样创建的用户,登录时的认证方式是密码.其实创建用户的语句还可以是: CREATE USER 'username'@'host' IDENTIFIED VIA 'pluginna

03、三种简单的计时器

1.计时器在游戏中的使用次数很多,以下是三种简单的计时器写法 2.代码: 1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 6 public class Timer : MonoBehaviour 7 { 8 private Text textTime; 9 private int second = 20; 10 11 pri

一个简单的游戏倒计时

一个简单的倒计时程序,60s倒数到0,然后返回60s继续倒数. 1 using UnityEngine; 2 using System.Collections; 3 4 public class Instantiate : MonoBehaviour { 5 public float targetTime = 60f; 6 public float currentTime = 0f; 7 // Use this for initialization 8 void Start () { 9 15

MySQL两个最简单的delimiter的使用Demo

今天复习MySQL,使用的工具是Navicat for MySQL,写了两个简单的delimiter的demo. 第一个,获取当前时间的年月日时分秒格式的时间: 1 delimiter $$ 2 drop function if exists fun_getDate$$ 3 create function fun_getDate(fdate datetime) returns varchar(100) 4 begin 5 declare result varchar(100) default '

简单的Hibernate访问数据库Demo

最近在学习SSH,现在看到Hibernate这块,动手实现了一个简单的Demo,对Hibernate的功能.使用有了初步了解. 1.首先将Hibernate的jar包复制到Web项目的lib目录下.有些依赖jar包,要额外导入:比如cglib-nodep.jar,不然会报错. 2.配置实体类.这里我用的是一个简单Account类,要注意使用的是javax.persistense.*下面的注解,不是org.hibernate.*下的. package com.jobhelp.domain; imp