我有这行代码,应该可以制作秒表。但是,当我运行该网页时,它被冻结在“ 60:10”。我什至不知道这些数字来自我的代码行。
它首先说存在递归问题,但我通过将(if hours < 1000)
更改为(if hours < 10)
来解决了这一问题。因此,也许这仍然是递归的事情。或者,Knack不允许更改这样的代码吗?我需要Ajax还是什么?
`
//testing javascript stopwatch
$(document).on('knack-view-render.view_2246', function(event, view, record){
Knack.showSpinner() //this is here only bc i've seen it elsewhere;
var count = 0;
var clearTime;
var seconds = 0, minutes = 0, hours = 0;
var clearState;
var mins, gethours;
$(".col-6").attr('id', 'shane')
function startWatch(){
mins = ( minutes < 10 ) ? ( '0' + minutes + ': ' ) : ( minutes + ': ' );
/* check if minutes is equal to 60 and add a +1 to hours set minutes to 0 */
if ( minutes === 60 ) { minutes = 0; hours = hours + 1; }
/* use the javascript tenary operator to format how the hours should look and add 0 to hours if less than 10 */
gethours = ( hours < 10 ) ? ( '0' + hours + ': ' ) : ( hours + ': ' );
minutes++;
if (hours < 10) {clearTime = setTimeout(startWatch(), 60000)}
$( ".col-6" ).html('Time: ' + gethours + ' ' + mins);
}
function startTime() {
if (minutes === 0 && hours === 0 ) {
startWatch(); }}
startWatch()
})
这行代码在控制台中不会产生任何错误消息,但不会像前面提到的那样移动时间。