一些团购网的倒计时如何在服务器端实现? 比如现在一些较为流行的团购网,糯米。拉手这样的,每个商品都有一个倒计时结束的效果,前台的倒计时是js脚本实现的,当时间为0的时候刷新一下页面,但是当刷新的时候,下一单新产品就已经发布了,说明他们在服务器端是不是也有个定时的方法啊,这个该怎么实现啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 团购网的出现大大的带动了ecshop网店市场,与时俱进的功能也层出不穷也带动了ecshop二次开发技术,很多客户需要倒计时代码,今天就和大家分享ecshop开发之如何实现团购网倒计时功能,以此来增加顾客购物热情和欲望。代码不难,大致分为三部分,获取当前时间,开始倒计时,提交。1.计算剩余时间的函数:function countDown(){ var i;var objArray =document.getElementsByTagName("label");//alert(objArray.length);for (i=0;i<(objArray.length);i++){if (objArray[i].id.indexOf("article_")>-1){objHid = document.getElementById("hid_article_" + objArray[i].id.substring(objArray[i].id.indexOf("_")+1));//objHid.value就是对应的时间值,然后改下面就行了var d=Date.parse(objHid.value);var today=new Date();var time=d-today;var 时间=objArray[i];if(Math.floor(time)<=0){时间.innerHTML='抢购时间已过,谢谢您的关注!';}else 时间没有过,就开始计算距离到期日的时间{var 天=Math.floor(time/(1000*60*60*24));var 小时=Math.floor(time/(1000*60*60))%24;var 分=Math.floor(time/(1000*60))%60;var 秒=Math.floor(time/1000)%60;时间.innerHTML=天+' 天'+小时+'小时'+分+'分'+秒+'秒';时间.style.fontSize='14px';}}}2.当然在计时函数之前必须知道当前时间,月份、小时、分、秒index.php/** 将GMT时间戳格式化为用户自定义时区日期*** @param string $format* @param integer $time 该参数必须是一个GMT的时间戳** @return string*/function local_date($format, $time = NULL){$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone']; if ($time === NULL){ $time = gmtime();}elseif ($time <= 0){ return '';}$time += ($timezone * 3600);return date($format, $time);}function chagetime($time){$time = local_date($GLOBALS['_CFG']['time_format'], $time);//echo $time;exit;$time = explode(" ",$time);//分割时间$date = $time[0];$time=$time[1];$date=explode("-",$date);$year = (int)$date[0];$month = (int)$date[1];$day = (int)$date[2];/*$time=explode(":",$time);$hour =$time[0];$min =$time[1];$sec =(int)$time[2];$hour = (int)(24-$hour);$min =(int)(60-$min);$time=$hour.":".$min.":".$sec;*/switch ($month) 这里是显示月份的函数{case 1:$month_name = "Jan";break;case 2:$month_name = "Feb";break;case 3:$month_name = "Mar";break;case 4:$month_name = "Apr";break;case 5:$month_name = "May";break;case 6:$month_name = "Jun";break;case 7:$month_name = "Jul";break;case 8:$month_name = "Aug";break;case 9:$month_name = "Sep";break;case 10:$month_name = "Oct";break;case 1:$month_name = "Nov";break;case 2:$month_name = "Dec";break;}return $month_name." ".$day." ".$year." ".$time; 返回时间值}3.这样就基本上完成了,最后输出值就可以了setTimeout('countDown()',1000);}countDown();</script><label id='article_1'></label><input type='hidden' id='hid_article_1' value='<?php echo chagetime(time());?>' /> 查了个php的因为你说会刷新我就想了个办法.if(Math.floor(time)<=0){时间.innerHTML='抢购时间已过,谢谢您的关注!';不是有这句么.同时刷新了页面.也就是说在刷新时在后置代码.cs中找到现在是不是这句,如果是就重新给JS中的新的数据. 用JS写到计时的脚本 当时间到0的时候提交页面 往后台发送一个POST请求 后台接受到这个请求后就会执行相应的操作 //以下内容不是回复楼主问题 public static void ToGrade(InPageAction IN, OutPageAction OUT) { if (OUT.Break || IN.ActionName != "tograde") { return; } uint moduleID = 0; uint infoID = 0; uint point = 0; uint nodeID = 0; string tableName = "WB_U_Comment"; IN.Page.HttpContext.GetRequest(ref moduleID, "infotypeid"); IN.Page.HttpContext.GetRequest(ref infoID, "infoid"); IN.Page.HttpContext.GetRequest(ref point, "point"); IN.Page.HttpContext.GetRequest(ref nodeID, "nodeid"); string ip = IN.Page.HttpContext.Context.Request.UserHostAddress.ToString(); string result = "2"; //2 查询 1 评分 0 失败 try { WB_Info wbInfo = new WB_Info(); wbInfo.TableName = tableName; wbInfo.Connection = IN.Page.Connection; if (point > 0) { if (!wbInfo.TrySelectFirstRow(new string[] { "InfoID" }, "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "' and IpAddress = '" + ip + "' and datediff('d',CreateDate,'" + DateTime.Now.ToString() + "')=0", "InfoID")) { //当天同个IP如果已经评分,将不再评分 wbInfo.Insert(new string[] { "ModuleID", "NewsID", "NodeID", "Point", "State", "TopIndex", "CreateDate", "IpAddress" }, new string[]{IN.Page.Connection.FormatTextFieldValue(moduleID.ToString()), IN.Page.Connection.FormatTextFieldValue(infoID.ToString()),IN.Page.Connection.FormatIntFieldValue(nodeID.ToString()),IN.Page.Connection.FormatTextFieldValue(point.ToString()), IN.Page.Connection.FormatIntFieldValue(0),IN.Page.Connection.FormatIntFieldValue(0),IN.Page.Connection.FormatDateFieldValue(DateTime.Now.ToString()),IN.Page.Connection.FormatTextFieldValue(ip)}); result = "1"; } } //得到当前评分 string avgPointSql = LC.Data.SelectValue.CreateAvgSqlString(IN.Page.Connection, tableName, "Point", "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "'"); string avgPoint = LC.Data.SelectValue.Execute(IN.Page.Connection, avgPointSql); if (avgPoint.Length > 4) { avgPoint = avgPoint.Substring(0, 4); } else if (string.IsNullOrEmpty(avgPoint)) { avgPoint = "0"; } //得到当前参与评分人数 string gradeNumSql = LC.Data.SelectValue.CreateCountSqlString(IN.Page.Connection, tableName, "InfoID", "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "'"); string gradeNum = LC.Data.SelectValue.Execute(IN.Page.Connection, gradeNumSql); OUT.Message = "[" + result + "," + avgPoint + "," + gradeNum + "]"; } catch (Exception ex) { result = "0"; OUT.Message = "[" + result + ",0," + ex.Message + "]"; } } 我看其他的一些网站,并没有发送post请求,前台的js仅仅是刷新了一下页面,所以我认为应该是后台也有一个定时的方法,在时间到了的时候,自动生成下一个商品的页面,覆盖掉原来的页面 其实连页面都不刷都可以做到if(Math.floor(time)<=0){时用JS调webservice里的方法重新生成新的html. 既然你是刷新一下页面,那就表示客户端向服务器端重新发出一个请求.后台得到请求,读取数据库数据库中对每一单都应该有一个开始的时间字段.譬如A单是9:00结束,其后紧排着B单,B单当然是9:00开始.你说的上一单A单倒计时为0时,当然就是9:00了,也就B单开始 ,后台读取数据时,可以在SQL语句中判断 select top 1 * where stime<=now FCK编辑器怎么自动过滤掉掉<html><head>标记内容了呢??? 为什么ClientScriptManager只执行了第一次啊 在线等 C#控件强制转换错误! 自定义控件属性问题.! html转成excel的问题 Table控件新增行的问题,简单地说就是点击一下增加一行(急)? 给大家提供个免费.net空间用用 在保存窗口里有一个保存按钮,我怎样让它点击后,提示保存成功,并关闭这个保存窗口 求教,关于ASP.NET引用dll的问题!! 子页面的问题????急!!!!!!在线等待 有人会用NetAdvantage的UltraWebTab控件吗 利用锚点的网络相册怎么做?
团购网的出现大大的带动了ecshop网店市场,与时俱进的功能也层出不穷也带动了ecshop二次开发技术,很多客户需要倒计时代码,今天就和大家分享ecshop开发之如何实现团购网倒计时功能,以此来增加顾客购物热情和欲望。代码不难,大致分为三部分,获取当前时间,开始倒计时,提交。1.计算剩余时间的函数:
function countDown(){ var i;var objArray =document.getElementsByTagName("label");//alert(objArray.length);for (i=0;i<(objArray.length);i++){
if (objArray[i].id.indexOf("article_")>-1){objHid = document.getElementById("hid_article_" + objArray[i].id.substring(objArray[i].id.indexOf("_")+1));//objHid.value就是对应的时间值,然后改下面就行了var d=Date.parse(objHid.value);var today=new Date();var time=d-today;
var 时间=objArray[i];if(Math.floor(time)<=0){时间.innerHTML='抢购时间已过,谢谢您的关注!';}else
时间没有过,就开始计算距离到期日的时间
{var 天=Math.floor(time/(1000*60*60*24));var 小时=Math.floor(time/(1000*60*60))%24;var 分=Math.floor(time/(1000*60))%60;var 秒=Math.floor(time/1000)%60;时间.innerHTML=天+' 天'+小时+'小时'+分+'分'+秒+'秒';时间.style.fontSize='14px';}}}
2.当然在计时函数之前必须知道当前时间,月份、小时、分、秒
index.php/** 将GMT时间戳格式化为用户自定义时区日期*** @param string $format* @param integer $time 该参数必须是一个GMT的时间戳** @return string*/
function local_date($format, $time = NULL){$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
if ($time === NULL){ $time = gmtime();}elseif ($time <= 0){ return '';}
$time += ($timezone * 3600);
return date($format, $time);}function chagetime($time){$time = local_date($GLOBALS['_CFG']['time_format'], $time);//echo $time;exit;$time = explode(" ",$time);//分割时间$date = $time[0];$time=$time[1];$date=explode("-",$date);$year = (int)$date[0];$month = (int)$date[1];$day = (int)$date[2];/*$time=explode(":",$time);$hour =$time[0];$min =$time[1];$sec =(int)$time[2];$hour = (int)(24-$hour);$min =(int)(60-$min);$time=$hour.":".$min.":".$sec;*/switch ($month) 这里是显示月份的函数{case 1:$month_name = "Jan";break;case 2:$month_name = "Feb";break;case 3:$month_name = "Mar";break;case 4:$month_name = "Apr";break;case 5:$month_name = "May";break;case 6:$month_name = "Jun";break;case 7:$month_name = "Jul";break;case 8:$month_name = "Aug";break;case 9:$month_name = "Sep";break;case 10:$month_name = "Oct";break;case 1:$month_name = "Nov";break;case 2:$month_name = "Dec";break;}return $month_name." ".$day." ".$year." ".$time; 返回时间值}3.这样就基本上完成了,最后输出值就可以了setTimeout('countDown()',1000);}countDown();</script>
<label id='article_1'></label><input type='hidden' id='hid_article_1' value='<?php echo chagetime(time());?>' />
if(Math.floor(time)<=0){时间.innerHTML='抢购时间已过,谢谢您的关注!';
不是有这句么.同时刷新了页面.
也就是说在刷新时在后置代码.cs中找到现在是不是这句,如果是就重新给JS中的新的数据.
{
if (OUT.Break || IN.ActionName != "tograde")
{
return;
}
uint moduleID = 0;
uint infoID = 0;
uint point = 0;
uint nodeID = 0;
string tableName = "WB_U_Comment";
IN.Page.HttpContext.GetRequest(ref moduleID, "infotypeid");
IN.Page.HttpContext.GetRequest(ref infoID, "infoid");
IN.Page.HttpContext.GetRequest(ref point, "point");
IN.Page.HttpContext.GetRequest(ref nodeID, "nodeid");
string ip = IN.Page.HttpContext.Context.Request.UserHostAddress.ToString();
string result = "2"; //2 查询 1 评分 0 失败
try
{
WB_Info wbInfo = new WB_Info();
wbInfo.TableName = tableName;
wbInfo.Connection = IN.Page.Connection;
if (point > 0)
{
if (!wbInfo.TrySelectFirstRow(new string[] { "InfoID" }, "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "' and IpAddress = '" + ip + "' and datediff('d',CreateDate,'" + DateTime.Now.ToString() + "')=0", "InfoID"))
{
//当天同个IP如果已经评分,将不再评分
wbInfo.Insert(new string[] { "ModuleID", "NewsID", "NodeID", "Point", "State", "TopIndex", "CreateDate", "IpAddress" }, new string[]{IN.Page.Connection.FormatTextFieldValue(moduleID.ToString()),
IN.Page.Connection.FormatTextFieldValue(infoID.ToString()),IN.Page.Connection.FormatIntFieldValue(nodeID.ToString()),IN.Page.Connection.FormatTextFieldValue(point.ToString()),
IN.Page.Connection.FormatIntFieldValue(0),IN.Page.Connection.FormatIntFieldValue(0),IN.Page.Connection.FormatDateFieldValue(DateTime.Now.ToString()),IN.Page.Connection.FormatTextFieldValue(ip)});
result = "1";
}
}
//得到当前评分
string avgPointSql = LC.Data.SelectValue.CreateAvgSqlString(IN.Page.Connection, tableName, "Point", "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "'");
string avgPoint = LC.Data.SelectValue.Execute(IN.Page.Connection, avgPointSql);
if (avgPoint.Length > 4)
{
avgPoint = avgPoint.Substring(0, 4);
}
else if (string.IsNullOrEmpty(avgPoint))
{
avgPoint = "0";
}
//得到当前参与评分人数
string gradeNumSql = LC.Data.SelectValue.CreateCountSqlString(IN.Page.Connection, tableName, "InfoID", "ModuleID = '" + moduleID + "' and NewsID ='" + infoID + "'");
string gradeNum = LC.Data.SelectValue.Execute(IN.Page.Connection, gradeNumSql);
OUT.Message = "[" + result + "," + avgPoint + "," + gradeNum + "]";
}
catch (Exception ex)
{
result = "0";
OUT.Message = "[" + result + ",0," + ex.Message + "]";
}
}
我看其他的一些网站,并没有发送post请求,前台的js仅仅是刷新了一下页面,所以我认为应该是后台也有一个定时的方法,在时间到了的时候,自动生成下一个商品的页面,覆盖掉原来的页面
其实连页面都不刷都可以做到
if(Math.floor(time)<=0){
时用JS调webservice里的方法重新生成新的html.
数据库中对每一单都应该有一个开始的时间字段.
譬如A单是9:00结束,其后紧排着B单,B单当然是9:00开始.
你说的上一单A单倒计时为0时,当然就是9:00了,也就B单开始 ,后台读取数据时,
可以在SQL语句中判断 select top 1 * where stime<=now