[关闭]
@jeffjade 2017-08-21T22:49:05.000000Z 字数 1947 阅读 1034

JavaScript 计时

javascript


  1. var _JadeWebSite = {}
  2. _JadeWebSite.setCurrentDatetime = function (data) {
  3. if (data.success === '1') {
  4. window.sessionStorage.setItem('current_timestamp', (+data.result.timestamp) * 1000)
  5. window.sessionStorage.setItem('current_datetime', data.result.datetime_1)
  6. } else {
  7. var currentDate = new Date()
  8. window.sessionStorage.setItem('current_timestamp', currentDate.getTime())
  9. window.sessionStorage.setItem('current_datetime', currentDate)
  10. }
  11. var nowDateTime = +(window.sessionStorage.getItem('current_timestamp'))
  12. _JadeWebSite.setRunTimeLong(nowDateTime)
  13. }
  14. _JadeWebSite.timerId = null
  15. _JadeWebSite.nowDateTime = null
  16. _JadeWebSite.setRunTimeLong = function (nowDateTime) {
  17. _JadeWebSite.updateRunTimeLong(nowDateTime)
  18. clearInterval(_JadeWebSite.timerId)
  19. _JadeWebSite.timerId = null
  20. _JadeWebSite.timerId = setInterval(function () {
  21. nowDateTime += 1000
  22. _JadeWebSite.updateRunTimeLong(nowDateTime)
  23. }, 1000)
  24. }
  25. _JadeWebSite.updateRunTimeLong = function (nowDateTime) {
  26. _JadeWebSite.nowDateTime = nowDateTime
  27. var startDateTime = +('1429383600000') // 2015-04-19 03:00
  28. var runDays = (nowDateTime - startDateTime) / 86400000
  29. if ($('#run_time_long').length > 0) {
  30. $('#run_time_long').html("本站共计运行&nbsp;<font style='color:purple'>" + runDays.toFixed(5) + "</font>&nbsp;天")
  31. }
  32. }
  33. function jsoncallbackFunc (data) {
  34. _JadeWebSite.setCurrentDatetime(data)
  35. }
  36. _JadeWebSite.getCurrentDatetime = function () {
  37. $.ajax({
  38. type: "GET",
  39. url: "http://api.k780.com:88/?app=life.time",
  40. data: "appkey=23548&sign=ed019c800dcfcdf09cdb79de51b4587d&format=json&jsoncallback=jsoncallbackFunc",
  41. dataType: "jsonp",
  42. success: function(data){
  43. // request callback via jsoncallbackFunc but no here @17-02-26
  44. _JadeWebSite.setCurrentDatetime(data)
  45. }
  46. })
  47. }
  48. window.onbeforeunload = function(e) {
  49. if (_JadeWebSite.nowDateTime) {
  50. window.sessionStorage.setItem('current_timestamp', _JadeWebSite.nowDateTime)
  51. }
  52. }
  53. if (!window.sessionStorage.getItem('current_timestamp')) {
  54. setTimeout(_JadeWebSite.getCurrentDatetime, 200)
  55. } else {
  56. var nowDateTime = +(window.sessionStorage.getItem('current_timestamp'))
  57. _JadeWebSite.setRunTimeLong(nowDateTime)
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注