Last active
August 29, 2015 14:26
-
-
Save ThomasChan/18cef44b03fc9ab6af0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [synchServerTime 弱心跳渐强同步服务器时间] | |
* @author ThomasChan | |
* @param {int} beginTime [当前时间] | |
* @param {int} endTime [结束时间] | |
* @param {function} iterator [每次同步完时间要做的事] | |
* @return | |
* @usage | |
* synchServerTime( | |
* +new Date() | |
* ,+new Date() + 3600 | |
* ,function(){ console.log(this.timeNow) } | |
* ); | |
* this will console 5 times timestamp | |
*/ | |
synchServerTime = function( beginTime, endTime, iterator ) { | |
var | |
obj = { | |
'timePublish' : endTime | |
,'timeNow' : beginTime | |
} | |
,timeGap = function() { return obj.timePublish - obj.timeNow } | |
,timeSynch = function( callbacks ) { itz.getTimeStamp(function( d ) { obj.timeNow = d.data,callbacks(); }.bind( obj )) } | |
// ,timeSynch = function( callbacks ) { /*ajax get server time && exec callbacks*/ } | |
,timerFn = function(){ | |
var timer = setTimeout( function() { | |
clearTimeout(timer) | |
,timeSynch( function() { | |
iterator.call(obj) | |
,reSynch() | |
}) | |
} ,timeGap() * 500 ) | |
} | |
,reSynch = function() { timeGap() >= 10 && timerFn() }; | |
reSynch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment