有新的订单,刷新并有提示音

来源:互联网 发布:linux 文件上传工具 编辑:程序博客网 时间:2024/04/29 07:20

pc端,有新的订单后,有提示音并刷新当前订单列表,主要用js 的setInterval 实现有间隔的循环刷新,再结合 cookie 和 session 达到效果

js

//创建cookiefunction setCookie(name,value) {     var Days = 30;     var exp = new Date();     exp.setTime(exp.getTime() + Days*24*60*60*1000);     document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } //获取cookiefunction getCookie(name) {     var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");    if(arr=document.cookie.match(reg))        return unescape(arr[2]);     else         return null; } //删除cookiefunction delCookie(name) {     var exp = new Date();     exp.setTime(exp.getTime() - 1);     var cval=getCookie(name);     if(cval!=null)         document.cookie= name + "="+cval+";expires="+exp.toGMTString(); } $(document).ready(function(){    dp_timer = setInterval('checkOrder()',60*1000);});function checkOrder(){  var lastCheckOrder = getCookie('ECS_LastCheckOrder');  var today = new Date();  if (lastCheckOrder == null || today-lastCheckOrder >= NEW_ORDER_INTERVAL)  {    setCookie('ECS_LastCheckOrder', Date.parse(new Date())/1000);    try    {        $.ajax({             url: '请求order.php里auto_call 方法',            dataType: "json",            success: function(data){              if(data.status == 1){                $('#embed').html("<embed src='order_ts.mp3' hidden='true'></embed>");                $('.cztable').html(data.html);              }else{                $('#embed').html('');              }            }        });        }    catch (e) { }  }}

order.php

 class order{  function order_list(){      $list = '订单列表数组';      //传递到模板      $GLOBALS['tmpl']->assign('list',$list);      //获得订单列表的模板html内容      $html = $GLOBALS['tmpl']->fetch('order_list.html');      return $html;  }  //有新的订单自动提示  function auto_call(){       $last_check = $_SESSION['last_check'];       if(!$last_check){           $_SESSION['last_check'] = time();           $last_check = es_session::get('last_check');       }       $sql = ' select count(*) from order where pay_time >= '.$last_check;       $new_orders = $GLOBALS['db']->getOne($sql);       $_SESSION['last_check'] = time();       $html = $this->order_list();       if($new_orders){          $result['ts'] = '<embed src="images/order_ts.mp3" hidden="true"></embed>';          $result['html'] = $html;          $result['status'] = 1;        }       else{          $result['status'] = 0;        }           //把result数组以json字符串的形式返回给js                 ajax_return($result);  } }

源地址:分析ecshop新订单提示原理

0 0
原创粉丝点击