gpt4 book ai didi

javascript - jquery:秒表

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:17:56 30 4
gpt4 key购买 nike

我正在使用我在此处找到的 stopwatch 代码: http://www.kellishaver.com/projects/stopwatch/

(function($) {
$.fn.stopwatch = function() {
var clock = this;
var timer = 0;

clock.addClass('stopwatch');

//console.log(clock);

// This is bit messy, but IE is a crybaby and must be coddled.
clock.html('<div class="display"><span class="hr">00</span>:<span class="min">00</span>:<span class="sec">00</span></div>');
clock.append('<input type="button" class="start" value="Start" />');
clock.append('<input type="button" class="stop" value="Stop" />');
clock.append('<input type="button" class="reset" value="Reset" />');

//console.log(clock.html());

// We have to do some searching, so we'll do it here, so we only have to do it once.
var h = clock.find('.hr');
var m = clock.find('.min');
var s = clock.find('.sec');
var start = clock.find('.start');
var stop = clock.find('.stop');
var reset = clock.find('.reset');

stop.hide();

start.bind('click', function() {
timer = setInterval(do_time, 1000);
stop.show();
start.hide();
});

stop.bind('click', function() {
clearInterval(timer);
timer = 0;
start.show();
stop.hide();
});

reset.bind('click', function() {
clearInterval(timer);
timer = 0;
h.html("00");
m.html("00");
s.html("00");
stop.hide();
start.show();
});

function do_time() {
// parseInt() doesn't work here...
hour = parseFloat(h.text());
minute = parseFloat(m.text());
second = parseFloat(s.text());

second++;

if(second > 59) {
second = 0;
minute = minute + 1;
}
if(minute > 59) {
minute = 0;
hour = hour + 1;
}

h.html("0".substring(hour >= 10) + hour);
m.html("0".substring(minute >= 10) + minute);
s.html("0".substring(second >= 10) + second);
}
};
})(jQuery);

我是这样使用它的:

<script type="text/javascript">
$('#clock1').stopwatch();
</script>

它工作正常,我可以使用停止按钮停止它。但是我希望能够使用 javascript 以编程方式停止它。像这样:

<script type="text/javascript">
$('#clock1').stop();
</script>

我创建了 stop 函数,但我无法访问 stopwatch() 中定义的 timer var。我该怎么做?

最佳答案

怎么样:

$('#clock1').find('.stop').trigger('click');

关于javascript - jquery:秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10065222/

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