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

目前总结解决方法:同时需要设置模块的函数,函数都可放置在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');

handleClick: util.throttle(function (e) { util.showLoading('加载中...'); let bol = this.checkCard(); // console.log(bol); if(bol){ this.bindFn(); this.getgrant("1"); wx.setStorageSync('verify', true); }else{ // 需要用户重新填写 util.hideLoading(); console.log('信息输入不正确'); } }, 3000),

即可。

你可能感兴趣的:(微信小程序遇坑——多次点击页面重复加载及数据重复提交)