商品倒计时打折js实现
2017-02-27 22:06
483 查看
function GetRTime() {
$(".mytime").each(function(){
//从页面拿到设置的日期格式例如为 2017-02-12 12:23:05
var mytime = $(this).html();
//将时间分别截取
var year = mytime.substring(0, 4);
var month = mytime.substring(5, 7);
var day = mytime.substring(8, 10);
var hour = mytime.substring(11, 13);
var mint = mytime.substring(14, 16);
var house = mytime.substring(17, 19);
var startTime = new Date();
var NowTime = new Date();
//获得当前的时间
startTime.setFullYear(year, month-1, day);
//调用设置年份,调用设置指定的时间的小时字段
startTime.setHours(hour);
//调用设置指定时间的分钟字段
startTime.setMinutes(mint);
//调用置指定时间的毫秒字段
startTime.setSeconds(house);
//获得截至的时间
var EndTime = startTime.getTime();
//截至时间减去当前时间获得剩余时间
var nMS = EndTime - NowTime.getTime();
//定义参数 获得天数
var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));
//定义参数 获得小时
var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
//定义参数 获得分钟
var nM = Math.floor(nMS / (1000 * 60)) % 60;
//定义参数,获得秒
var nS = Math.floor(nMS / 1000) % 60;
//如果天数,小时,分钟为负,则显示为0
if(nD<=0){
nD=0;
};
if(nH<=0){
nH=0;
};
if(nM<=0){
nM=0;
};
//这些就是当前时间
$(this).next().html("特价菜倒计时"+nD + "天" + nH + "时" + nM + "分" + nS + "秒");
//特价时间在当前时间之前显示,并打折该商品
if(nMS>0){
//商品原价
var $yuanprice=$(this).parent().parent().parent().find(".span4").html();
$(this).next().show();
var $yuanjia=$yuanprice*0.8;
//alert($yuanjia.toFixed(2));
$(this).parent().parent().parent().find(".huodongjia").html($yuanjia.toFixed(2)).css("color","red");
$(this).parent().parent().parent().find(".huodongjia").show();
$(this).parent().parent().parent().find(".huodongzi").show();
}else{
var $yuanprice=$(this).parent().parent().parent().find(".span4").html();
//alert($yuanjia.toFixed(2));
$(this).parent().parent().parent().find(".huodongjia").html($yuanprice);
};
});
}
$(document).ready(function() {
var timer_rt = window.setInterval("GetRTime()", 1000);
)};
前台jsp部分代码
<c:forEach items="${requestScope.showShopsMes}" var="showShopsMes">
<div id="div9">
<span>${showShopsMes.name}</span>
</div>
<div id="div10">
<img src="static/image/xingji4.gif" alt=""/>
</div>
<div id="div11">
<p>配送费</p>
<span>配送费¥${showShopsMes.sendPrice}</span>
</div>
<div id="div12">
<p>商家地址</p>
<span>${showShopsMes.address}</span>
</div>
</c:forEach>
$(".mytime").each(function(){
//从页面拿到设置的日期格式例如为 2017-02-12 12:23:05
var mytime = $(this).html();
//将时间分别截取
var year = mytime.substring(0, 4);
var month = mytime.substring(5, 7);
var day = mytime.substring(8, 10);
var hour = mytime.substring(11, 13);
var mint = mytime.substring(14, 16);
var house = mytime.substring(17, 19);
var startTime = new Date();
var NowTime = new Date();
//获得当前的时间
startTime.setFullYear(year, month-1, day);
//调用设置年份,调用设置指定的时间的小时字段
startTime.setHours(hour);
//调用设置指定时间的分钟字段
startTime.setMinutes(mint);
//调用置指定时间的毫秒字段
startTime.setSeconds(house);
//获得截至的时间
var EndTime = startTime.getTime();
//截至时间减去当前时间获得剩余时间
var nMS = EndTime - NowTime.getTime();
//定义参数 获得天数
var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));
//定义参数 获得小时
var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;
//定义参数 获得分钟
var nM = Math.floor(nMS / (1000 * 60)) % 60;
//定义参数,获得秒
var nS = Math.floor(nMS / 1000) % 60;
//如果天数,小时,分钟为负,则显示为0
if(nD<=0){
nD=0;
};
if(nH<=0){
nH=0;
};
if(nM<=0){
nM=0;
};
//这些就是当前时间
$(this).next().html("特价菜倒计时"+nD + "天" + nH + "时" + nM + "分" + nS + "秒");
//特价时间在当前时间之前显示,并打折该商品
if(nMS>0){
//商品原价
var $yuanprice=$(this).parent().parent().parent().find(".span4").html();
$(this).next().show();
var $yuanjia=$yuanprice*0.8;
//alert($yuanjia.toFixed(2));
$(this).parent().parent().parent().find(".huodongjia").html($yuanjia.toFixed(2)).css("color","red");
$(this).parent().parent().parent().find(".huodongjia").show();
$(this).parent().parent().parent().find(".huodongzi").show();
}else{
var $yuanprice=$(this).parent().parent().parent().find(".span4").html();
//alert($yuanjia.toFixed(2));
$(this).parent().parent().parent().find(".huodongjia").html($yuanprice);
};
});
}
$(document).ready(function() {
var timer_rt = window.setInterval("GetRTime()", 1000);
)};
前台jsp部分代码
<c:forEach items="${requestScope.showShopsMes}" var="showShopsMes">
<div id="div9">
<span>${showShopsMes.name}</span>
</div>
<div id="div10">
<img src="static/image/xingji4.gif" alt=""/>
</div>
<div id="div11">
<p>配送费</p>
<span>配送费¥${showShopsMes.sendPrice}</span>
</div>
<div id="div12">
<p>商家地址</p>
<span>${showShopsMes.address}</span>
</div>
</c:forEach>
相关文章推荐
- JS实现商品倒计时
- JS-实现商品倒计时
- JS实现商品倒计时
- JS实现商品倒计时实现代码
- JS实现商品倒计时实现代码
- PHP加JS实现商品秒杀倒计时
- js实现时间段的倒计时显示
- js实现考试时间倒计时
- JS 倒计时实现代码(时、分,秒)
- js实现倒计时(转)
- 同一页面多个商品倒计时JS 基于面向对象的javascript
- JS实现的倒计时(时、分、秒)
- js是实现页面倒计时跳转
- 用js实现倒计时(js代码)
- JS实现网站倒计时
- 简单的JS实现按钮触发倒计时功能
- asp.net中js调用服务端方法,服务端调用JS,实现无刷新的倒计时
- JS实现倒计时(时、分,秒)
- 使用JS实现倒计时(在页面上动态的显示)
- JS实现倒计时