我有一个使用js和一些php片段的数字计数器。每4秒,数字计数器的值增加+1。柜台的运行开始时间是早上7点到晚上7点。我希望在晚上7点得到最后的计数器。我会在一定时间内显示该值。
你能帮助实现这个目标吗? enter link description here
PHP
$date = date('G:i', time());
$server_time = strtotime($date);
$initial_time = 1493949600;
$target_time = 1512007200;
$current_data_ = $row->cust_count + ($server_time - $initial_time)/($target_time - $initial_time) * 250000;
JS
(function ($){
$.fn.countTo = function (options){
options = options || {};
return $(this).each(function (){
//set options for current element
var settings = $.extend({}, $.fn.countTo.defaults,{
from: $(this).data('from'),
to: $(this).data('to'),
speed: $(this).data('speed'),
//refreshInterval: $(this).data('refresh-interval'),
decimals: $(this).data('decimals')
}, options);
var loops = Math.ceil(settings.speed / settings.refreshInterval),
increment = (settings.to - settings.from) / loops; //how many times to update the value, and how much to increment the value on each update
//references & variables that will change with each update
var self = this,
$self = $(this),
loopCount = 0,
value = settings.from,
data = $self.data('countTo') || {};
$self.data('countTo', data);
//if an existing interval can be found, clear it first
if (data.interval){
clearInterval(data.interval);
}
data.interval = setInterval(updateTimer, settings.refreshInterval);
render(value); //initialize the element with the starting value
function updateTimer(){
value += increment;
loopCount++;
render(value);
if (typeof(settings.onUpdate) == 'function'){
settings.onUpdate.call(self, value);
}
if (loopCount >= loops){
//remove the interval
$self.removeData('countTo');
clearInterval(data.interval);
value = settings.to;
if(typeof(settings.onComplete) == 'function'){
settings.onComplete.call(self, value);
}
}
}
function render(value){
var formattedValue = settings.formatter.call(self, value, settings);
$self.html(formattedValue);
$self.html(value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ' '));
}
});
};
$.fn.countTo.defaults ={
from: 0, //the number the element should start at
to: 0, //the number the element should end at
speed: 1000, //how long it should take to count between the target numbers
refreshInterval: 100, //how often the element should be updated
decimals: 0, //the number of decimal places to show
formatter: formatter, //handler for formatting the value before rendering
onUpdate: null, //callback method for every time the element is updated
onComplete: null //callback method for when the element finishes updating
};
function formatter(value, settings){
return value.toFixed(settings.decimals);
}
}(jQuery));
jQuery(function($){
//custom formatting example
$('.count-number').data('countToOptions',{
formatter: function (value, options){
return value.toFixed(options.decimals).replace(/\B(?=(?:\d{3})+(?!\d))/g, ',');
}
});
// $('.timer').each(count); //start all the timers
function count(options){
var $this = $(this);
options = $.extend({}, options || {}, $this.data('countToOptions') || {});
$this.countTo(options);
}
});
var curr_date = new Date()
var current_time = (new Date).getTime()/1000
var current_hour = (new Date).getHours()
var current_minutes = curr_date.getMinutes()
var current_seconds = curr_date.getSeconds()
var curr_time = current_hour + ":" + current_minutes + ":" + current_seconds
var speed = 60*60*24*30*3*40
//800000 is the value of $row->cust_count
var current_data = 800000 + (current_time - initial_time)/(target_time - initial_time) * 250000;
}
switch((new Date).getTime()){
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
jQuery(function($){
if(curr_time >= '07:00:00' && curr_time <= '19:00:00'){
$('.timer').countTo({
from: current_data,
to: 1000000,
speed: speed,
refreshInterval: 1000,
onUpdate: function(value){
console.debug(this);
},
onComplete: function(value){
console.debug(this);
}
});
} else if(curr_time >= '17:00:01' && curr_time <= '6:59:59'){
$('.timer').countTo({
from: 800000, //here will be display the last count of the counter
to: 800000 //here will be display the last count of the counter
});
}
}(jQuery));