Vor kurzem machen wir das Lotterieprojekt von Arrangement Five. In jeder Periode verfügt über eine Lotterie -Kaufzeit. Das heißt, wenn der Benutzer diese Anordnung fünf Seite öffnet, wird eine Remaintime vom Server gesendet (die verbleibende Zeit vor dem Ende dieser Lotteriezeit), und dann wird dem Benutzer auf der Clientseite diese Zeit präsentiert, sodass der Benutzer die verbleibende Zeit dieser Lotterienzeit erhalten kann.
Das Implementierungsprinzip ist recht einfach. Ich werde hier nicht auf Details eingehen. Führen Sie den folgenden Code aus, um die Demo anzuzeigen:
<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=gbk" /><title>index</title><style type="text/css">em{color:#f00;}</style></head><body><div id="remaintime"></div><script type = "text/javaScript"> var thetime = function () {this.init.Apply (this, argumente);}; thetime.prototype = {init: function (obj) {var that = this this; obj = das.buildparam (obj); that.callback = obj.callback; var container = that.container = document.getElementById (obj.container); Container.InnerHtml = '<em> </em> stunde <em> </em> minute <em> </em> Sekunden'; var stundenspace = that.hourspace = container.getElementsByTagName ('em') [0]; var mutenpace = that.minutespace = container.getElementsByTagName ('em') [1]; var secondspace = that.secondspace = container.getElementsByTagName ('em') [2]; if (obj.remaintime == 0) {that.resettime (); zurückkehren; } that.hours = math.floor (obj.Remaintime/3600); that._remain1 = obj.Remaintime % 3600; that.minutes = math.floor (that._remain1/60); that.seconds = that._remain1 % 60; var timer = that.timer = setInterval (function () {that.rendertime.apply (that);}, 1000); }, BuildParam: Funktion (obj) {obj = {// Container ist der ID -Container des DOM -Knotens: obj.Container || 'Container', bleibt: Nummer (obj.Remaintime) || 0, // Rückruf nach Abschluss des Countdowns Rückruf: obj.callback || neue Funktion}; Rückkehr obj; }, Reettime: function () {var that = this; that.container.innerhtml = "Deadlined"; }, // Zeit -Rendertime: function () {// Debugger; var das = dies; if (that.seconds> 0) {this.seconds--; } else {that.seconds = 59; if (that.minutes> 0) {this.minutes--; } else {that.minutes = 59; if (this.hours> 0) {this. stunden--; }}} // Standby if (that.hours == 0 && that.minutes == 0 && that.seconds == 0) {// Callback that._callback (); } var bithandle = that.bitHandle; var _Hour = bitHandle (die Stunde); var _minute = bitHandle (das.minutes); var _second = bitHandle (that.seconds); that.hourspace.innerhtml = _Hour; that.minutespace.innerhtml = _minute; that.secondspace.innerhtml = _second; }, // Für Bit-Verarbeitung senden Sie sicher, dass eine zweistellige Zahl bithandle: function (num) {var str = num.toString (); if (Str.Length <2) {str = 0 + str; } return str; }, _callback: function () {var that = this; ClearInterval (this.timer); that.callback (); }}; Neue Thetime ({// Container -ID -Container: 'Remaintime', // Die verbleibende Zeit, die vom Server zurückgegeben wird