Files
chatai/src/utils/tools.js
2023-11-10 17:13:55 +08:00

52 lines
1.3 KiB
JavaScript

/**
* 复制到剪切板
*/
export function copyToClipboard(content) {
const clipboardData = window.clipboardData
if (clipboardData) {
clipboardData.clearData()
clipboardData.setData('Text', content)
return true
} else if (document.execCommand) {
const el = document.createElement('textarea')
el.value = content
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
return true
}
return false
}
/**
* 浅拷贝
* @param any
* @returns {any}
*/
export const scopy = (any = null) => JSON.parse(JSON.stringify(any))
export const load = (ctx, params = {c: 0, delay: 500}) => {
if (ctx) {
return Promise.resolve(ctx)
} else if (params.c < 10) {
return new Promise(resolve => setTimeout(() => resolve(load({...params, c: ++params.c}), params.delay)))
} else return Promise.reject("无法加载内容")
}
export const addJs = url => {
const script = document.createElement("script")
script.src = url
document.body.appendChild(script)
}
export const throttle = (fn, delay = 1000) => {
if (typeof fn != "function") return;
if (window.throttleTimer) clearTimeout(window.throttleTimer)
window.throttleTimer = setTimeout(() => fn.call(), delay)
}