I am currently copying a text to clipboard using the following code:
navigator.clipboard.writeText(element.value).then(function() {
/* clipboard successfully set */
}, function() {
/* clipboard write failed */
console.log('Copy to clipboard failed!');
});
I need to clear the clipboard automatically after 1 minute if the data is successfully copied to the clipboard.
I tried using setTimeout()
method but write to clipboard is failing when called from setTimeout
.
Also, if I am using execCommand('copy')
method, I am getting the error:
document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.
I am trying to make it work in Firefox.
The code I am trying to put inside setTimeout() is as follows:
setTimeout(function() {
navigator.clipboard.writeText('').then(function() {
/* Successfully cleared clipboard */
}, function() {
/* Failed to clear clipboard */
}
}, 60000);