The question is about hiding a message after 5 seconds.
In the code below, if I click on the Generate Room Name button , it will generate the url in the textbox. I will be copying that url using the Copy button and a “Copied to clipboard” message will be displayed. I want to hide that message after 5 seconds. Please someone look into it.
JavaScript
x
53
53
1
import React from 'react';
2
import FlashMessage from 'react-flash-message'
3
import Iframe from 'react-iframe';
4
export default class CustomView extends React.Component {
5
constructor(props) {
6
super(props);
7
this.state = {
8
roomname: '',
9
copySuccess: '',
10
enablebutton: true
11
}
12
}
13
makeid() {
14
let r = (Math.random() + 1).toString(36).substring(7);
15
console.log("random", r);
16
this.setState({
17
roomname: r,
18
enablebutton: false
19
})
20
}
21
copyToClipboard = (e) => {
22
this.textArea.select();
23
document.execCommand('copy');
24
e.target.focus();
25
this.setState({ copySuccess: 'Copied!' });
26
};
27
render() {
28
return (
29
<div>
30
<button onClick={this.makeid.bind(this)}> Generate RoomName</button>
31
<div style={{ display: "flex", marginLeft: '19%', marginTop: '-2%' }}>
32
<form disabled={this.state.enablebutton}>
33
<textarea style={{
34
width: "457px",
35
height: "15px"
36
}} disabled={this.state.enablebutton}
37
ref={(textarea) => this.textArea = textarea}
38
value={`https://xxxxxxxxx.azurewebsites.net/?roomname=${this.state.roomname}`}/>
39
</form>
40
{
41
document.queryCommandSupported('copy') &&
42
<div disabled={this.state.enablebutton}>
43
<button onClick={this.copyToClipboard}>Copy</button>
44
<p style={{ color: "red" }}> {this.state.copySuccess}</p>
45
</div>
46
}
47
</div>
48
49
</div>
50
);
51
}
52
}
53
Thanks
Advertisement
Answer
It’s enough to clear the copySuccess
state after 5000 ms:
JavaScript
1
7
1
copyToClipboard = (e) => {
2
this.textArea.select();
3
document.execCommand('copy');
4
e.target.focus();
5
this.setState({ copySuccess: 'Copied!' }, () => setTimeout( () => this.setState({ copySuccess: '' }) ,5000));
6
};
7