我正在尝试开发一个秒表,但我有问题增加我的计数使用for和while声明请有人打电话给我去哪里帮我修复我的代码,我的问题是,一次间隔后时间我设置它在1秒内从1增加到10但我希望它每秒加一个。
var count=0;
var s = 0;
setInterval(function(){
while(count < 10){
$("#mi").html(count);
count++;
}
},1000);
//i also use for but both give did same thing
setInterval(function(){
for(count=0;count < 10;count++){
$("#mi").html(count);
}
},1000);
<div id="mi">0</div>
答案 0 :(得分:0)
以下代码有效。为什么要使用循环?如果要点是每秒递增一次,请执行以下操作:
import store from ../store
ipc.on('someevent', (event, args) => {
// do stuff with args
store.commit('update-things')
})
如果你想将它增加10,那么
import store from '../store'
const {ipcMain} = require('electron')
const WebTorrent = require('webtorrent')
const client = new WebTorrent()
ipcMain.on('addMagnet', (event, arg) => {
client.add(arg, function (torrent) {
var files = []
torrent.files.forEach(function (file) {
files.push({
title: file.name,
torrent: torrent.infoHash,
index: torrent.files.indexOf(file),
duration: '--:--'
})
})
store.commit('addSongs', files)
})
答案 1 :(得分:0)
您可以使用clearInterval
来中断计时器。
clearInterval()方法清除使用setInterval()设置的计时器 方法
setInterval()返回的ID值用作参数 clearInterval()方法。
所以试试这个:
var count=0;
var tHnd = setInterval(function(){
if(count == 10)
clearInterval(tHnd);
$("#mi").html(count);
count++;
},1000);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="mi"></div>
&#13;