[英]How to call a setInterval function
Instead of setInterval
running upon refresh, I'd like it to run upon clicking on the start button & go up to time.而不是
setInterval
在刷新时运行,我希望它在单击开始按钮和 go 时运行。 I tried almost every variation for the past hours & it didn't work without any errors.在过去的几个小时里,我几乎尝试了所有变体,但它没有任何错误。
Instead the timer stops which makes sense if time > 2. Therefore, I am not sure how I can make timer restart once start
gets click and for the timer to stop once time reached (ie 2 seconds)相反,如果时间> 2,则计时器停止,这很有意义。因此,我不确定如何在开始点击后重新
start
计时器,并让计时器在达到时间(即 2 秒)后停止
HTML HTML
<script src='https://api.chipware.co.za/js/flipclock-min.js'></script><script src="./script.js"></script>
<button style="width:200px; height: 50px;" id="start">Start</button>
<button style="width:200px; height: 50px;"onclick="submit()" id="stop">Submit</button>
JS JS
let time = 2;
var clock = $('.clock').FlipClock(0, {
clockFace: 'HourlyCounter',
countdown: false });
function submit(){
clock.stop();
}
var element_ = document.getElementById("start");
element_.addEventListener('click', function(){
start(time);
});
function start(time){
countup = setInterval(function () {
if(clock.getTime().time > time) {
clock.stop();
clearInterval(countup);
}
else{
var element = document.getElementById("stop");
element.addEventListener('click', function(){
submit();
});
}
})};
Also unsure why this code runs w/o error by itself but in chrome extension, it gives:也不确定为什么此代码本身运行时没有错误,但在 chrome 扩展中,它给出:
Uncaught TypeError: $(...).FlipClock is not a function
Your missing flipclock library I just added and update the code now it work.我刚刚添加并更新了您缺少的翻转时钟库,现在它可以工作了。
let time = 2; var clock = $('.clock').FlipClock(0, { clockFace: 'HourlyCounter', countdown: false }); function submit(){ clock.stop(); } var element_ = document.getElementById("start"); element_.addEventListener('click', function(){ start(time); }); function start(time){ countup = setInterval(function () { console.log(clock.getTime().time,time) if(clock.getTime().time > time) { clock.stop(); clearInterval(countup); } else{ var element = document.getElementById("stop"); element.addEventListener('click', function(){ submit(); }); } })};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/flipclock/0.7.7/flipclock.min.js" integrity="sha512-Vy4ftjkcjamOFPNSK7Osn8kYhF7XDcLWPiRvSmdimNscisyC8MkhDlAHSt+psegxRzd/q6wUC/VFhQZ6P2hBOw==" crossorigin="anonymous"></script> <button style="width:200px; height: 50px;" id="start">Start</button> <button style="width:200px; height: 50px;"onclick="submit()" id="stop">Submit</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.