Skip to content
Advertisement

Random Img with start button

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
JavaScript

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
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement