@jeffjade
2017-08-21T22:49:05.000000Z
字数 1947
阅读 1034
javascript
var _JadeWebSite = {}
_JadeWebSite.setCurrentDatetime = function (data) {
if (data.success === '1') {
window.sessionStorage.setItem('current_timestamp', (+data.result.timestamp) * 1000)
window.sessionStorage.setItem('current_datetime', data.result.datetime_1)
} else {
var currentDate = new Date()
window.sessionStorage.setItem('current_timestamp', currentDate.getTime())
window.sessionStorage.setItem('current_datetime', currentDate)
}
var nowDateTime = +(window.sessionStorage.getItem('current_timestamp'))
_JadeWebSite.setRunTimeLong(nowDateTime)
}
_JadeWebSite.timerId = null
_JadeWebSite.nowDateTime = null
_JadeWebSite.setRunTimeLong = function (nowDateTime) {
_JadeWebSite.updateRunTimeLong(nowDateTime)
clearInterval(_JadeWebSite.timerId)
_JadeWebSite.timerId = null
_JadeWebSite.timerId = setInterval(function () {
nowDateTime += 1000
_JadeWebSite.updateRunTimeLong(nowDateTime)
}, 1000)
}
_JadeWebSite.updateRunTimeLong = function (nowDateTime) {
_JadeWebSite.nowDateTime = nowDateTime
var startDateTime = +('1429383600000') // 2015-04-19 03:00
var runDays = (nowDateTime - startDateTime) / 86400000
if ($('#run_time_long').length > 0) {
$('#run_time_long').html("本站共计运行 <font style='color:purple'>" + runDays.toFixed(5) + "</font> 天")
}
}
function jsoncallbackFunc (data) {
_JadeWebSite.setCurrentDatetime(data)
}
_JadeWebSite.getCurrentDatetime = function () {
$.ajax({
type: "GET",
url: "http://api.k780.com:88/?app=life.time",
data: "appkey=23548&sign=ed019c800dcfcdf09cdb79de51b4587d&format=json&jsoncallback=jsoncallbackFunc",
dataType: "jsonp",
success: function(data){
// request callback via jsoncallbackFunc but no here @17-02-26
_JadeWebSite.setCurrentDatetime(data)
}
})
}
window.onbeforeunload = function(e) {
if (_JadeWebSite.nowDateTime) {
window.sessionStorage.setItem('current_timestamp', _JadeWebSite.nowDateTime)
}
}
if (!window.sessionStorage.getItem('current_timestamp')) {
setTimeout(_JadeWebSite.getCurrentDatetime, 200)
} else {
var nowDateTime = +(window.sessionStorage.getItem('current_timestamp'))
_JadeWebSite.setRunTimeLong(nowDateTime)
}