JSFiddle - React, Tailwind, and code Playground
HTML
<button>Copy a random number</button>
CSS
intervalinterval94 19 undefinedundefinedundefined53 53 53 53 80undefined57
85858542
13
JavaScript
var toCopy;
const buttonClick = () => {
setTimeout(function() {
if (toCopy) {
// strangely, this execCommand will work
console.log(document.execCommand('copy'));
}
}, 1000);
var client = new XMLHttpRequest();
client.onload = function(data) {
console.log(this);
toCopy = this.responseText;
};
// by setting your timeout on your request
// with the same duration as the setTiemout
// you make sure it either works or throws an error
client.ontimeout = function(data) {
console.log('timeout');
};
client.timeout = 1000;
client.open("GET", "https://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain");
client.send();
}
$(() => {
$("button").click(buttonClick)
})
document.addEventListener('copy', function(e) {
e.preventDefault();
e.clipboardData.setData('text/plain', toCopy);
});