From 306151ca5834ec794ce34cb17ded4a4643c4ba99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=BC=E5=A5=A5?= <1988wangxiao@gmail.com> Date: Wed, 22 Apr 2015 14:10:49 +0800 Subject: [PATCH] =?UTF-8?q?[feature]=20Demo2=20=E6=98=BE=E7=A4=BA=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8F=91=E9=80=81=E7=9A=84=E6=97=B6=E9=97=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/demo2/test.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/demo/demo2/test.js b/demo/demo2/test.js index ffafae85a..2336c2708 100644 --- a/demo/demo2/test.js +++ b/demo/demo2/test.js @@ -133,7 +133,7 @@ function sendMsg() { // 发送成功之后的回调 inputSend.value = ''; - showLog('自己: ' , val); + showLog('(' + formatTime(data.t) + ') 自己: ', val); printWall.scrollTop = printWall.scrollHeight; }); @@ -171,7 +171,7 @@ function showMsg(data, isBefore) { if (data.fromPeerId === clientId) { from = '自己'; } - showLog(from + ': ', text, isBefore); + showLog('(' + formatTime(data.timestamp) + ') ' + from + ': ', text, isBefore); } // 拉取历史 @@ -202,7 +202,9 @@ function getLog(callback) { for (var i = l - 1; i >= 0; i --) { showMsg(data[i], true); } - printWall.scrollTop = printWall.scrollHeight - height; + if (l) { + printWall.scrollTop = printWall.scrollHeight - height; + } if (callback) { callback(); } @@ -234,6 +236,16 @@ function encodeHTML(source) { // .replace(/'/g,'''); } +function formatTime(time) { + var date = new Date(time); + var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1; + var currentDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); + var hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); + var mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); + var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); + return date.getFullYear() + '-' + month + '-' + currentDate+' '+ hh + ':' + mm + ':' + ss; +} + function bindEvent(dom, eventName, fun) { if (window.addEventListener) { dom.addEventListener(eventName, fun);