<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>
标签:now,vue,return,节流,textArea,content,lastCall,document,函数
From: https://www.cnblogs.com/LiZiheng/p/18145934