gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
function copyToTextWithClipboard(text, ms, count)
- User
function copyToTextWithClipboard(text, ms, count)
- Assistant
Certainly! Here's an example implementation of the
copyToTextWithClipboardfunction, which copies a giventextto the clipboard repeatedlycounttimes with a delay ofmsmilliseconds between each copy operation:function copyToTextWithClipboard(text, ms, count) { if (!navigator.clipboard) { console.error('Clipboard API not supported'); return; } let copied = 0; function copyOnce() { if (copied >= count) return; // Stop if done navigator.clipboard.writeText(text) .then(() => { console.log(`Copied ${copied + 1} times`); copied++; setTimeout(copyOnce, ms); }) .catch(err => { console.error('Failed to copy text: ', err); }); } copyOnce(); }Explanation:
- Checks if the Clipboard API is supported.
- Defines a helper function
copyOncewhich:- Copies the text
- Increments the
copiedcounter - Schedules the next copy after
msmilliseconds usingsetTimeout, untilcountis reached.
Example usage:
copyToTextWithClipboard("Hello, World!", 1000, 5);This will copy "Hello, World!" to the clipboard 5 times, one time per second.
Feel free to ask if you'd like a version using async/await or other variations!