最近、私たちはアレンジ5の宝くじプロジェクトを行っています。各期間には宝くじの購入期間があります。つまり、ユーザーがこのアレンジメント5ページを開くと、サーバーから償却が送信され(この宝くじの終了前の残りの時間)、今回はクライアント側のユーザーに提示され、ユーザーはこの宝くじの残りの時間を取得できます。
実装の原則は非常に簡単です。ここでは詳しく説明しません。次のコードを実行してデモを表示します。
<!doctype html> <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = gbk"/> <title> index </title> <style type = "text/css"> em {color:#f00;} type = "text/javascript"> var thetime = function(){this.init.apply(this、arguments);}; thetime.prototype = {init:function(obj){var this = this; obj = that.buildparam(obj); that.callback = obj.callback; var container = that.container = document.getElementById(obj.container); container.innerhtml = '<em> </em> hour <em> </em> minute <em> </em>秒'; var hourspace = that.hourspace = container.getElementsByTagname( 'em')[0]; var minutespace = that.minutespace = container.getElementsByTagname( 'em')[1]; var secondspace = that.secondspace = container.getElementsByTagname( 'em')[2]; if(obj.remaintime == 0){that.resettime();戻る; } that.hours = math.floor(obj.remaintime/3600); that._remainder1 = obj.remaintime%3600; that.minutes = math.floor(that._remainder1/60); that.seconds = that._Remainder1%60; var timer = that.timer = setInterval(function(){that.rendertime.apply(that);}、1000); }、buildParam:function(obj){obj = {//コンテナはdomノードのIDコンテナです:obj.container || 「コンテナ」、残り:number(obj.remaintime)|| 0、//カウントダウンが完了した後のコールバックコールバック:obj.callback ||新しい関数}; OBJを返します。 }、resettime:function(){var that = this; that.container.innerhtml = "Deadlined"; }、//更新時間rendertime:function(){// debugger; var that = this; if(that.seconds> 0){that.seconds--; } else {that.seconds = 59; if(that.minutes> 0){that.minutes--; } else {that.minutes = 59; if(that.hours> 0){that.hours--; }}} //スタンバイif(that.hours == 0 && that.minutes == 0 && that.seconds == 0){// callback that._callback(); } var bithandle = that.bithandle; var _hour = bithandle(that.hours); var _minute = bithandle(that.minutes); var _second = bithandle(that.seconds); that.hourspace.innerhtml = _hour; that.minutespace.innerhtml = _minute; that.secondspace.innerhtml = _second; }、//ビット処理については、2桁の数字ビットハンドルを返してください:function(num){var str = num.toString(); if(str.length <2){str = 0 + str; } return str; }、_callback:function(){var that = this; ClearInterval(that.timer); that.callback(); }}; new thetime({// container id container: 'Remaintime'、//サーバーによって返される残りの時間、ユニットは残り時間:10000、//カウントダウンが完了したときにコールバック{document.getElementbyid( 'remaintime')。innerhtml = 'deadline';}); </script>