gpt4 book ai didi

javascript - 如何使用 JavaScript 创建秒表?

转载 作者:IT王子 更新时间:2023-10-29 03:19:45 25 4
gpt4 key购买 nike

if(stopwatch >= track[song].duration)

track[song].duration 查找音云轨道的持续时间。

我想创建一个秒表功能,当您点击 swap ID stopwatch 时开始计时if 函数将做某事的时间量。在我的例子中替换图像。并且该功能将在再次点击时自行重置。

stopwatch = current time - clicked time 如何设置clicked time

当前时间 = new Date().getTime(); ?这是以毫秒为单位吗?

$('#swap').click(function()...

最佳答案

您会看到演示代码只是一个启动/停止/重置 毫秒计数器。如果您想当时进行奇特的格式化,那完全取决于您。这应该足以让您入门。

这是一个有趣的小项目。这是我的处理方式

var Stopwatch = function(elem, options) {

var timer = createTimer(),
startButton = createButton("start", start),
stopButton = createButton("stop", stop),
resetButton = createButton("reset", reset),
offset,
clock,
interval;

// default options
options = options || {};
options.delay = options.delay || 1;

// append elements
elem.appendChild(timer);
elem.appendChild(startButton);
elem.appendChild(stopButton);
elem.appendChild(resetButton);

// initialize
reset();

// private functions
function createTimer() {
return document.createElement("span");
}

function createButton(action, handler) {
var a = document.createElement("a");
a.href = "#" + action;
a.innerHTML = action;
a.addEventListener("click", function(event) {
handler();
event.preventDefault();
});
return a;
}

function start() {
if (!interval) {
offset = Date.now();
interval = setInterval(update, options.delay);
}
}

function stop() {
if (interval) {
clearInterval(interval);
interval = null;
}
}

function reset() {
clock = 0;
render(0);
}

function update() {
clock += delta();
render();
}

function render() {
timer.innerHTML = clock / 1000;
}

function delta() {
var now = Date.now(),
d = now - offset;

offset = now;
return d;
}

// public API
this.start = start;
this.stop = stop;
this.reset = reset;
};


// basic examples
var elems = document.getElementsByClassName("basic");

for (var i = 0, len = elems.length; i < len; i++) {
new Stopwatch(elems[i]);
}


// programmatic examples
var a = document.getElementById("a-timer");
aTimer = new Stopwatch(a);
aTimer.start();

var b = document.getElementById("b-timer");
bTimer = new Stopwatch(b, {
delay: 100
});
bTimer.start();

var c = document.getElementById("c-timer");
cTimer = new Stopwatch(c, {
delay: 456
});
cTimer.start();

var d = document.getElementById("d-timer");
dTimer = new Stopwatch(d, {
delay: 1000
});
dTimer.start();
.stopwatch {
display: inline-block;
background-color: white;
border: 1px solid #eee;
padding: 5px;
margin: 5px;
}

.stopwatch span {
font-weight: bold;
display: block;
}

.stopwatch a {
padding-right: 5px;
text-decoration: none;
}
<h2>Basic example; update every 1 ms</h2>

<p>click <code>start</code> to start a stopwatch</p>

<pre>
var elems = document.getElementsByClassName("basic");

for (var i=0, len=elems.length; i&lt;len; i++) {
new Stopwatch(elems[i]);
}
</pre>
<div class="basic stopwatch"></div>
<div class="basic stopwatch"></div>

<hr>
<h2>Programmatic example</h2>

<p><strong>Note:</strong> despite the varying <code>delay</code> settings, each stopwatch displays the correct time (in seconds)</p>

<pre>
var a = document.getElementById("a-timer");
aTimer = new Stopwatch(a);
aTimer.start();
</pre>
<div class="stopwatch" id="a-timer"></div>1 ms<br>

<pre>
var b = document.getElementById("b-timer");
bTimer = new Stopwatch(b, {delay: 100});
bTimer.start();
</pre>
<div class="stopwatch" id="b-timer"></div>100 ms<br>

<pre>
var c = document.getElementById("c-timer");
cTimer = new Stopwatch(c, {delay: 456});
cTimer.start();
</pre>
<div class="stopwatch" id="c-timer"></div>456 ms<br>

<pre>
var d = document.getElementById("d-timer");
dTimer = new Stopwatch(d, {delay: 1000});
dTimer.start();
</pre>
<div class="stopwatch" id="d-timer"></div>1000 ms<br>

为它获取一些基本的 HTML 包装器

<!-- create 3 stopwatches -->
<div class="stopwatch"></div>
<div class="stopwatch"></div>
<div class="stopwatch"></div>

从那里开始使用非常简单

var elems = document.getElementsByClassName("stopwatch");

for (var i=0, len=elems.length; i<len; i++) {
new Stopwatch(elems[i]);
}

作为奖励,您还可以获得定时器的可编程 API。这是一个用法示例

var elem = document.getElementById("my-stopwatch");
var timer = new Stopwatch(elem, {delay: 10});

// start the timer
timer.start();

// stop the timer
timer.stop();

// reset the timer
timer.reset();

jQuery 插件

至于 jQuery 部分,一旦你有了像上面那样好的代码组合,编写一个 jQuery 插件 就很简单了

(function($) {
var Stopwatch = function(elem, options) {
// code from above...
};

$.fn.stopwatch = function(options) {
return this.each(function(idx, elem) {
new Stopwatch(elem, options);
});
};
})(jQuery);

jQuery 插件使用:

// all elements with class .stopwatch; default delay (1 ms)
$(".stopwatch").stopwatch();

// a specific element with id #my-stopwatch; custom delay (10 ms)
$("#my-stopwatch").stopwatch({delay: 10});

jsbin.com demo

关于javascript - 如何使用 JavaScript 创建秒表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com