vue中节流函数的使用

<template>
    <div>
          <div>{{content}}</div>
          <el-button size="small" icon="icon iconfont ps-fuzhi" type="primary" @click="testCopy()">
               复制
          </el-button>

    </div>
</template>

<script>

export default {
    data() {
        return {
            content:"测试数据",
            testCopy:this.throttle(this.copy,2000)
        };
    },
    methods: {
        //节流函数
        throttle(func, delay) {
            let lastCall = 0;
            return function(...args) {
                const now = new Date().getTime();
                if (now - lastCall < delay) {
                    this.$message.warning("请勿频繁操作")
                    return;
                }
                lastCall = now;
                return func.apply(this, args);
            };
        },
        //复制
        copy() {
            let textArea = document.createElement("textarea");
            textArea.value = this.content
            document.body.appendChild(textArea);
            textArea.select()
            document.execCommand('copy')
            document.body.removeChild(textArea)
            this.$message.success('复制成功!')
        },
    },
    created() {
    }
};
</script>


posted @ 2024-04-19 15:12  Code_Lzh  阅读(61)  评论(0)    收藏  举报