欢迎光临,了解微信小程序开发,就上易用通!

微信小程序+PHP:动态显示项目倒计时(格式:4天7小时58分钟39秒) ...

发布:2018-02-02 10:19浏览: 来源:网络 作者:cola

1、一般我们说的显示秒杀都是指的单条数据,循环我没做。

效果:

微信小程序+PHP:动态显示项目倒计时(格式:4天7小时58分钟39秒) ...(图1)

2、wxml代码:

 

  1. <p><block wx:if="{{total_micro_second<=0}}">剩余时间:已经截止</block>
  2. <block wx:if="{{clock!='已经截止'}}">剩余时间:{{clock}}</block>
  3. </p>

3、.js文件代码:

 

  1. function countdown(that) {
  2. var EndTime = that.data.end_time || [];
  3. var NowTime = new Date().getTime();
  4. var total_micro_second = EndTime - NowTime || [];
  5. console.log('剩余时间:' + total_micro_second);
  6. // 渲染倒计时时钟
  7. that.setData({
  8. clock: dateformat(total_micro_second)
  9. });
  10. if (total_micro_second <= 0) {
  11. that.setData({
  12. clock: "已经截止"
  13. });
  14. //return;
  15. }
  16. setTimeout(function () {
  17. total_micro_second -= 1000;
  18. countdown(that);
  19. }
  20. , 1000)
  21. }
  22.  
  23. // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  24. function dateformat(micro_second) {
  25. // 总秒数
  26. var second = Math.floor(micro_second / 1000);
  27. // 天数
  28. var day = Math.floor(second/3600/24);
  29. // 小时
  30. var hr = Math.floor(second/3600%24);
  31. // 分钟
  32. var min = Math.floor(second/60%60);
  33. // 秒
  34. var sec = Math.floor(second%60);
  35. return day + "天" + hr + "小时" + min + "分钟" + sec+"秒";
  36. }
  37.  
  38. Page({
  39.  
  40. /**
  41. * 页面的初始数据
  42. */
  43. data: {
  44. id:'',
  45. result:[],
  46. end_time:'',
  47. clock:''
  48. },/**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. var that = this;
  53. wx.request({
  54. url: 'https://m.******.com/index.php/Home/Xiaoxxf/activity_detail?a_id='+options.id,//不含富文本html
  55. data: {},
  56. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  57. header: {
  58. 'Content-Type': 'application/json'
  59. },
  60. success: function (res) {
  61. that.setData({
  62. common: res.data, //一维数组,全部数据
  63. end_time: res.data.end_time //项目截止时间,时间戳,单位毫秒
  64. })
  65. console.log(that.data.common);
  66. console.log('结束时间:' + that.data.end_time);
  67. },
  68. fail: function (res) { },
  69. complete: function (res) { },
  70. }),
  71. //调用上面定义的递归函数,一秒一刷新时间
  72. countdown(that);
  73. },

4、拿去用吧,不懂的下面留言。





免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。