我有一个函数,当用户输入数据以设置里程表时调用(计数器变量中脚本的底部)。如果用户想要更改任何字段,他们只需在表单中输入一些新数据,里程表就会重置并重新启动。
我遇到的问题是以前的功能(jOdometer)仍在使用以前的数据以及新设置的里程表运行。如何停止以前的功能运行并重新启动?
我希望我能做到以下...
$(function(){
var counter;
$('#submit').click(function(){
if(counter != undefined){
counter.stop();
}
// get values from user.
var counter_start = 0;
var counter_end = 0;
var increment = 0;
var delayTime = 0;
var speed = 0;
var extraDigits = [];
if($('#counter_end').length > 0){
counter_end = $('#counter_end').val();
}else{
counter_end = false;
}
if($('#counter_start').length > 0){
counter_start = $('#counter_start').val();
if(counter_end.length > counter_start.length){
for(i=counter_start.length+1; i<=counter_end.length; i++){
extraDigits = '0' + extraDigits;
}
if(extraDigits.length > 0){
counter_start = extraDigits + counter_start
}
}
}else{
counter_start = 1;
}
if($('#increment').length > 0){
increment = $('#increment').val();
}else{
increment = 1;
}
if($('#delay_time').length > 0){
delayTime = $('#delay_time').val();
}else{
delayTime = 500;
}
if($($('#delay_time')).length > 0){
delayTime = $('#delay_time').val();
}else{
delayTime = 1000;
}
counter = $('.counter4').jOdometer({
counterStart: String(counter_start),
counterEnd: String(counter_end),
delayTime: parseInt(delayTime),
increment: parseInt(increment),
speed: parseInt(speed),
formatNumber: true,
spaceNumbers: 0,
widthDot: 10
});
})
});
然而不起作用。我究竟做错了什么?