Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

function copyToTextWithClipboard(text, ms, count)

  1. User

    function copyToTextWithClipboard(text, ms, count)

  2. Assistant

    Certainly! Here's an example implementation of the copyToTextWithClipboard function, which copies a given text to the clipboard repeatedly count times with a delay of ms milliseconds 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 copyOnce which:
      • Copies the text
      • Increments the copied counter
      • Schedules the next copy after ms milliseconds using setTimeout, until count is 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!