微信小程序遇坑——多次点击页面重复加载及数据重复提交

微信小程序遇坑——多次点击页面重复加载及数据重复提交,第1张

目前总结解决方法:同时需要设置模块拆猜的函数,函数都可放置在util.js中去。

首先:

一、在util.js中放卜运入如下两组函数

1. 设置点击后多久不能再次 *** 作该

function throttle(fn, gapTime) {

  if (gapTime == null || gapTime == undefined) {

    gapTime = 1500

  }

  let _lastTime = null

  // 返回新的函数

  return function () {

    let _nowTime = + new Date()

    if (_nowTime - _lastTime >gapTime || !_lastTime) {

      fn.apply(this, arguments)  //将this和参数传给原函数

      _lastTime = _nowTime

    }

  }

}

2. 设置加载动画

function showLoading(message) {

  if (wx.showLoading) {    // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理

    wx.showLoading({

      title: message, mask: true

    })

  } else {    // 低版本型御梁采用Toast兼容处理并将时间设为20秒以免自动消失

    wx.showToast({

      title: message, icon: 'loading', mask: true, duration: 20000

    })

  }

}

function hideLoading() {

  if (wx.hideLoading) {    // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理

    wx.hideLoading()

  } else {

    wx.hideToast()

  }

}

并且将其导出作为页面使用:

module.exports = {

  throttle: throttle,

  showLoading: showLoading,

  hideLoading: hideLoading,

}

二、将函数引入页面使用

const util = require('../../utils/util.js')

即可。

在点击事件中添加一碧闭个Boolean型变量

事件函数{

var b=true

if (b==true){

添加数据

b=false

}

if(b==false){

删悔大裂除数据

b=true

}

}

原理就是在事件函仿咐数中加一个开关,控制着存放和删除


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11968726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存