您的位置:首页 > Web前端 > JavaScript

商品倒计时打折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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: