E企盈营销工具技术服务商 热线:4006-838-530

小程序setInterval定时器问题

E企盈直播平台营销卖货系统

A. 操作步骤:商城–》秒杀商品详情页,显示倒计时–》下单支付,未支付–》商品订单详情页,显示支付倒计时–》返回到秒杀商品详情页,该倒计时功能已失效/**   * 倒计时   * endtime //截止时间(格式处理如yyyy-MM-dd hh:mm:ss或者yyyy/MM/dd hh:mm:ss)   * paramname //绑定数据对象名称(选填)   * isdayformat  //是否进行”n天hh:mm:ss”格式返回(选填)   * invalidcb  //倒计时失效后的回调   */  setIntervalTime: function (_that, endtime, paramname, isdayformat,invalidcb) {    var seconds = dateUtil.getSecondDiff(endtime);    var that = this;    if (seconds = 0) {      var timeval = {        timestr: ’00:00:00′,        h: ’00’,        m: ’00’,        s: ’00’      };      var dataobj = {        isinvalid: true,        timeval: timeval      };      if (paramname) {        var obj = {};        var currst = _that.data[paramname];        currst[“isinvalid”] = dataobj.isinvalid;        currst[“timeval”] = dataobj.timeval;        obj[paramname] = currst;        _that.setData(obj);      } else {        _that.setData(dataobj);      }      typeof invalidcb == “function” && invalidcb();      return;    }    var timeval = dateUtil.transTime(seconds, isdayformat);    var dataobj = {      timeval: timeval    };    if (paramname) {      var obj = {};      var currst = _that.data[paramname];      currst[“isinvalid”] = dataobj.isinvalid;      currst[“timeval”] = dataobj.timeval;      obj[paramname] = currst;      _that.setData(obj);    } else {      _that.setData(dataobj);    }    if (this.globalData.intervalId) {      clearInterval(that.globalData.intervalId);    }    this.globalData.intervalId = setInterval(function () {            seconds = seconds – 1;      timeval = dateUtil.transTime(seconds, isdayformat);      console.log(“enter—–setInterval:”, seconds);      var dataobj = {        timeval: timeval      };      if (paramname) {        var obj = {};        var currst = _that.data[paramname];        currst[“isinvalid”] = dataobj.isinvalid;        currst[“timeval”] = dataobj.timeval;        obj[paramname] = currst;        _that.setData(obj);      } else {        _that.setData(dataobj);      }      if (seconds = 0) {        clearInterval(that.globalData.intervalId);        that.globalData.intervalId = null;        timeval = {          timestr: ’00:00:00′,          h: ’00’,          m: ’00’,          s: ’00’        };        var dataobj = {          isinvalid: true,          timeval: timeval        };        if (paramname) {          var obj = {};          var currst = _that.data[paramname];          currst[“isinvalid”] = dataobj.isinvalid;          currst[“timeval”] = dataobj.timeval;          obj[paramname] = currst;          _that.setData(obj);        } else {          _that.setData(dataobj);        }        typeof invalidcb == “function” && invalidcb();      }    }, 1000);  },B. 项目中存在的问题:该小程序中用到的倒计时,是全局设置的一个倒计时变量,因此只要生成了一个倒计时,同时也会覆盖掉之前所有打开页面的倒计时,在项目运行过程中会一直都执行(备注:页面内定义的倒计时,也是在项目运行过程中一直执行)C. 解决方案:在页面的onhide(进入新页面,隐藏该页面)事件和onunload(关闭当前页面)事件中,清除全局倒计时;在页面的onshow(重新进入或者返回到该页面)事件中,根据条件重新设置全局倒计时。注意:清除倒计时,clearInterval(intervalid);intervalid = null;  //必须加这一条,因为clearInterval以后,intervalid还是有值的

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 小程序setInterval定时器问题
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏