Javascript 加载DOM时的时间戳问题
我正在为圆形仪表(CSS3随着边界半径、旋转和剪辑而增长)编写一个jQuery小部件。我在页面上有很多CSS/images/js 如果我在DOM加载时执行小部件代码:Javascript 加载DOM时的时间戳问题,javascript,jquery,timestamp,Javascript,Jquery,Timestamp,我正在为圆形仪表(CSS3随着边界半径、旋转和剪辑而增长)编写一个jQuery小部件。我在页面上有很多CSS/images/js 如果我在DOM加载时执行小部件代码: $(document).ready(function(){ $(".gauge").gauge(); }); 我有一些时间戳问题,我的代码不能正常运行。 timestamp timerFinish始终低于新的timestamp,因此秒变量为负,百分比始终高于120/150,因此代码永远不会执行。。。 这是因为_create函
$(document).ready(function(){
$(".gauge").gauge();
});
我有一些时间戳问题,我的代码不能正常运行。
timestamp timerFinish始终低于新的timestamp,因此秒变量为负,百分比始终高于120/150,因此代码永远不会执行。。。
这是因为_create函数一次运行一次,而第一个setInterval(the _stopjaugefn)在之后运行了很长时间
但如果我这样做:
$(window).load(function(){
$(".gauge").gauge();
});
代码是有效的
问题在于时间戳之间的差异
如果有人对这种行为有解释的话
代码如下:
(function($){
$.widget("as.gauge", {
options: {
timerSeconds: 3
},
_create: function() {
var self = this;
this._startTimer();
},
_startTimer: function(){
var self = this;
this.timerFinish = new Date().getTime() + (this.options.timerSeconds * 1000);
log('Timerfinish : ' + this.timerFinish)
this.timer = setInterval(function(){
log("milli : "+new Date().getMilliseconds())
self._stopJauge();
}, 50);
},
_endTimer: function(){
clearInterval(this.timer);
},
_drawTimer: function(percent) {
var deg = 360 / 100 * percent;
percent = Math.round(percent);
this.element.html('<div class="percent"></div><div id="slice"' +
(percent > 50 ? ' class="gt50"' : '') +
'><div class="pie"></div>' +
(percent > 50 ? '<div class="pie fill"></div>' : '') +
'</div>'
).find('#slice .pie').css({
'-moz-transform' : 'rotate(' + deg + 'deg)',
'-webkit-transform' : 'rotate(' + deg + 'deg)',
'-o-transform' : 'rotate(' + deg + 'deg)',
'transform' : 'rotate(' + deg + 'deg)'
});
this.element.find('.percent').html('<span class="int">' + percent + '%</span>');
},
_stopJauge: function(data) {
var seconds = (this.timerFinish - new Date().getTime()) / 1000,
percent = 100 - ((seconds / this.options.timerSeconds) * 100);
log('stopJauge - getTime : ' + new Date().getTime());
log('timerfinish - new Date() = ' + (this.timerFinish - new Date().getTime()))
log('seconds : ' + seconds)
log('percent : ' + percent)
if (percent <= this.element.data('value')){
this._drawTimer(percent);
} else {
this.element.find('.percent .int').html(this.element.data('value') + '%');
this._endTimer();
}
}
});
}(jQuery));
(函数($){
$.widget(“as.gauge”{
选项:{
时间秒:3
},
_创建:函数(){
var self=这个;
这个;
},
_startTimer:function(){
var self=这个;
this.timerFinish=new Date().getTime()+(this.options.timerSeconds*1000);
日志('Timerfinish:'+this.Timerfinish)
this.timer=setInterval(函数(){
日志(“milli:+newdate().getmillides())
self._stopJauge();
}, 50);
},
_结束计时器:函数(){
clearInterval(这个计时器);
},
_drawTimer:函数(百分比){
var deg=360/100*百分比;
百分比=数学四舍五入(百分比);
this.element.html(“”+
(百分比>50?“”:“”)+
''
).find('#slice.pie').css({
“-moz变换”:“旋转(“+deg+”deg)”,
“-webkit变换”:“旋转(“+deg+”deg)”,
“-o变换”:“旋转(“+deg+”deg)”,
“变换”:“旋转(“+deg+”deg)”
});
this.element.find('.percent').html(''+percent+'%');
},
_stopJauge:函数(数据){
var seconds=(this.timerFinish-new Date().getTime())/1000,
百分比=100-((秒/this.options.timerSeconds)*100);
日志('stopJauge-getTime:'+新日期().getTime());
日志('timerfinish-new Date()='+(this.timerfinish-new Date().getTime()))
日志('秒:'+秒)
日志('百分比:'+百分比)
如果(百分比),请定义“我的代码执行不正确。”。可能是一个有趣的阅读。