I have a simple code here showing some random images every time it is refreshed & 2 buttons mainly Display Random Image & Stop. How can I set a timer of 2 seconds every time a user clicks on the Display Random Image & stop the recurrence when stop is click?
JavaScript
x
33
33
1
var imageUrls = [
2
"https://cdn.pixabay.com/photo/2020/11/18/19/41/cathedral-5756535_640.jpg",
3
"https://cdn.pixabay.com/photo/2018/05/28/18/35/sahara-3436700_960_720.jpg",
4
"https://cdn.pixabay.com/photo/2020/10/14/22/32/buildings-5655593_640.jpg",
5
"https://cdn.pixabay.com/photo/2020/07/20/12/08/social-distancing-5422795_640.jpg",
6
"https://cdn.pixabay.com/photo/2020/11/22/16/58/road-5767221_640.jpg",
7
"https://cdn.pixabay.com/photo/2016/09/10/02/20/photography-1658471_640.jpg"
8
];
9
10
function getImageHtmlCode() {
11
var dataIndex = Math.floor(Math.random() * imageUrls.length);
12
var img = '<a href="' + '"><img src="';
13
img += imageUrls[dataIndex];
14
img += '" alt="Image Error"/></a>';
15
return img;
16
}
17
18
var start = false;
19
var numChange;
20
21
function startButton() {
22
if (document.getElementById("go")) {
23
numChange = setInterval(getImageHtmlCode, 1000);
24
}
25
}
26
27
function stopButton() {
28
if (start == true) {
29
start = false;
30
}
31
}
32
33
document.write(getImageHtmlCode());
JavaScript
1
2
1
<input type="button" id="go" onclick="startButton()" value="Display Random Image">
2
<button onclick="stopButton()">Stop</button><br/>
Advertisement
Answer
Have a look at this
It is not recommended to use document.write
You do not need a start boolean when you can set and clear the interval
I just change the source of the image to avoid manipulating the DOM after initialising the image
JavaScript
1
34
34
1
var imageUrls = [
2
"https://cdn.pixabay.com/photo/2020/11/18/19/41/cathedral-5756535_640.jpg",
3
"https://cdn.pixabay.com/photo/2018/05/28/18/35/sahara-3436700_960_720.jpg",
4
"https://cdn.pixabay.com/photo/2020/10/14/22/32/buildings-5655593_640.jpg",
5
"https://cdn.pixabay.com/photo/2020/07/20/12/08/social-distancing-5422795_640.jpg",
6
"https://cdn.pixabay.com/photo/2020/11/22/16/58/road-5767221_640.jpg",
7
"https://cdn.pixabay.com/photo/2016/09/10/02/20/photography-1658471_640.jpg"
8
];
9
10
function getImage() {
11
var dataIndex = Math.floor(Math.random() * imageUrls.length);
12
return imageUrls[dataIndex];
13
}
14
15
var numChange;
16
17
function startButton() {
18
clearInterval(numChange); // just make sure it is cleared before setting it again
19
numChange = setInterval(show, 1000);
20
}
21
22
function stopButton() {
23
clearInterval(numChange)
24
}
25
26
function show() {
27
document.getElementById("image").src = getImage()
28
}
29
30
window.addEventListener("load", function() { // when page loads
31
document.getElementById("imageDiv").innerHTML = '<a href=""><img id="image" src="' + getImage() + '" alt="Image Error"/></a>'
32
document.getElementById("go").addEventListener("click", startButton);
33
document.getElementById("stop").addEventListener("click", stopButton);
34
})
JavaScript
1
3
1
<button type="button" id="go">Display Random Image</button>
2
<button type="button" id="stop">Stop</button><br/>
3
<div id="imageDiv"></div>