vue 前端节流

/**

    * 函数节流    方法封装

    * @param fn

    * @param interval

    * @returns {Function}

    * @constructor

    */

    Throttle (fn, t) {

        let last

        let timer

        let interval = t || 500

        return function () {

            let args = arguments

            let now = +new Date()

            if (last && now - last < interval) {

                clearTimeout(timer)

                timer = setTimeout(() => {

                    last = now

                    fn.apply(this, args)

                }, interval)

            } else {

                last = now

                fn.apply(this, args)

            }

        }

    }

//  方法使用:

EG:  (外加二次弹框确认)

 fluteBeputin: util.Throttle(function () {

                let arr = []

                arr.push(arg0.productId)

                this.$confirm('操作将选中的产品加入产品库,请确认是否执行?', '确认信息', {

                    distinguishCancelAndClose: true,

                    confirmButtonText: '确认',

                    cancelButtonText: '取消'

                })

                    .then(() => {

                        let params = {

                            productIdList:arr,

                        }

                        util.request({

                            method: 'post',

                            interface: 'aaaaa',

                            data: params

                        }).then(res => {

                            if (res.result.success == '1') {

                                this.$message.success(res.result.message)

                            } else {

                                this.$message.error(res.result.message)

                            }

                        })

                    })

                    .catch(action => {

                        this.$message({

                            type: 'info',

                            message: action === 'cancel'

                                ? '您已取消入库'

                                : '停留在当前页面'

                        })

                });

            // },       

            }, 3000),

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容