Home  >  Article  >  Web Front-end  >  Javascript implements flash sale, group purchase and countdown display record sharing_javascript skills

Javascript implements flash sale, group purchase and countdown display record sharing_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:29:071135browse

Recently I did a flash sale on real estate, an e-commerce website for group purchases (there are also flash sales on houses, the price is not small), there is a countdown display of the flash sale, mainly to determine how much time is left between the current time and the start of the flash sale, and also There are various displays showing the start of the flash sale and the end of the flash sale.
The most important point is that the so-called current time cannot use the client time obtained by the browser through new Date(). In this way, as long as the user modifies his or her machine time, the countdown will be messed up, so the current time must be The server time is used, so a static cache page is used, so the current time is obtained using ajax
Javascript implements flash sale, group purchase and countdown display record sharing_javascript skills

Copy code The code is as follows:




< /title><br><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><br><meta http-equiv="Content-Language" content=" zh-CN" /><br><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /><br><meta content="all" name="robots" /><br><meta name="author" content="" /><br></head><br><body onload='start()'><br>.<br>There are still days until the event starts: <span id="sk_time"></span> <!-- This is the countdown display--><br>.<br><br/><br><span id="wyz"><br><span class="btn_02"> Participate in the flash sale! ! ! </span> <!-- This is a flash kill button. When the countdown reaches 0, it will become a flash kill style--><br></span><br>.<br><script type= "text/javascript"><br>var msbegintime = "1323446400000"; //This is the timestamp when the activity starts<br>var msendtime = "1325174400000"; //This is the timestamp when the activity ends<br>function start (){<br> callBackServerTime("sk_time", "wyz", msbegintime, msendtime);<br>}<br>//_showtimediv: time display area, _showqdiv: status display area<br>//This is sent to the server An ajax request, the server returns the current timestamp of the server, that is, xmlobj.responseText is a timestamp of the server<br>function callBackServerTime(_showtimediv, _showqdiv, _ms_begintime, _ms_endtime) {<br> var now = new Date();<br> var urlstr = "random=" Math.round(Math.random() * 10000000);<br> var ajaxobj = new AJAXRequest; // Create an AJAX object<br> ajaxobj.method = "GET"; // Settings The request method is GET<br> ajaxobj.url = "/gz/source/getServerTime.do?" urlstr; //Pay attention to the cross-domain issues of ajax<br> ajaxobj.callback = function(xmlobj) {<br> //ShowQTime (xmlobj.responseText, _showtimediv, _showqdiv, _ms_begintime, _ms_endtime, _tryid,sourceid); j.responseText is convenient for testing<br> }<br> ajaxobj.send(); // Send request<br>}<br>//Dynamic display of "flash sale" time function<br>function ShowQTime(_showtimediv, _showqdiv, _nowtime, _ms_begintime, _ms_endtime) {<br> _nowtime = Number(_nowtime);<br> var timmer = Math.floor((_ms_endtime - _nowtime) / (1000)); <br> if (_nowtime >= _ms_begintime && timmer > 0) {;<br> // Flash sale in progress <br> document.getElementById(_showtimediv).innerHTML = "<span class='pim_time'>0</span>days<span class='pim_time'>0</span>hours <span class='pim_time'>0</span>minutes<span class='pim_time'>0</span>seconds";<br>                document.getElementById(_showqdiv).innerHTML = "<span class='btn_01'><a href='/gz/sk/v/'> The flash sale has begun! ! !</a></span>";<br> } else {<br>                                                                                                                                                                                                            > var nD = Math.floor(nMS / (1000 * 60 * 60 * 24));<br> var nH = Math.floor(nMS / (1000 * 60 * 60)) % 24;<br> var nM = Math.floor(nMS / (1000 * 60)) % 60;<br> var nS = Math.floor(nMS / 1000) % 60;<br> var nMS = Math.floor(nMS / 100) % 10;<br> if (nD >= 0) {<br> var _timestr = "";<br> var snd = nD.toString();<br> if (snd.length == 1) {<br>            snd = "0" snd;<br> }<br> _timestr = "<span class='pim_time'>" snd.substring(0, 1) snd.substring(1, 2) "</span>day" ;<br> var snH = nH.toString();<br> if (snH.length == 1) {<br> snH = "0" snH;<br> }<br> _timestr = "<span class ='pim_time'>" snH.substring(0, 1) snH.substring(1, 2) "</span>hour";<br>        var snM = nM.toString();<br>                               .length == 1) {<br>                                                                                    1, 2) "</span>minutes";<br> var snS = nS.toString();<br> if (snS.length == 1) {<br> snS = "0" snS;<br>                                                                                                               (_showtimediv).innerHTML = _timestr;<br>                                                                                                                                                                 day <span class='pim_time'>0</span>hours<span class='pim_time'>0</span>minutes<span class='pim_time'>0</span>seconds"; <br>               document.getElementById(_showqdiv).innerHTML = "<span class='btn_01'><a href='/gz/sk/v/'> The flash sale is over! ! !  </a></span>";<br>        }<br>    }<br>    //注意 (_nowtime 1000) 增加 1 秒<br>    setTimeout("ShowQTime('" _showtimediv "','" _showqdiv "','" (_nowtime 1000) "','" _ms_begintime "','" _ms_endtime "')", 1000);<br>}<br>function AJAXRequest() {<br>    var xmlObj = false;<br>    var CBfunc,ObjSelf;<br>    ObjSelf=this;<br>    try { xmlObj=new XMLHttpRequest; }<br>    catch(e) {<br>        try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }<br>        catch(e2) {<br>            try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }<br>            catch(e3) { xmlObj=false; }<br>        }<br>    }<br>    if (!xmlObj) return false;<br>    this.method="POST";<br>    this.url;<br>    this.async=true;<br>    this.content="";<br>    this.callback=function(cbobj) {return;}<br>    this.send=function() {<br>        if(!this.method||!this.url||!this.async) return false;<br>        xmlObj.open (this.method, this.url, this.async);<br>        if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<br>        xmlObj.onreadystatechange=function() {<br>            if(xmlObj.readyState==4) {<br>                if(xmlObj.status==200) {<br>                    ObjSelf.callback(xmlObj);<br>                }<br>            }<br>        }<br>        if(this.method=="POST") xmlObj.send(this.content);<br>        else xmlObj.send(null);<br>    }<br>}<br></script><br></body><br></html><br> </div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="Three ways for js jquery to get server controls with randomly generated IDs_jquery" href="http://m.php.cn/faq/16747.html">Three ways for js jquery to get server controls with randomly generated IDs_jquery</a></span><span>Next article:<a class="dBlack" title="Three ways for js jquery to get server controls with randomly generated IDs_jquery" href="http://m.php.cn/faq/16749.html">Three ways for js jquery to get server controls with randomly generated IDs_jquery</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="http://m.php.cn/about/us.html">About us</a><a href="http://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="http://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>