I have a countdown timer on my website, and I want it to overlay a generic explosion GIF when the timer runs out. I have a function call when it runs out, now I just need to know what will go in that function.
I’m really new to JavaScript and web dev as a whole, so I don’t have any idea how to go about this.
I’ve tried looking online but all of the docs I’ve found don’t provide any help or don’t make any sense.
This is the code that I have so far:
window.onload = function () {
var time = 299, // your time in seconds here
display = document.querySelector('#timer');
startTimer(time, display);
};
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
// This is the part that will call the explosion function.
}
}, 1000);
}
6
Answers
nice job setting up the timer! to overlay an explosion GIF when the timer runs out, you just need to add an image element with the GIF and style it to appear over the content when the countdown hits 0.
so, your updated startTimer function will look something like this:
this will make the GIF take over the whole screen when the timer hits 0. Let me know if it works
It can be achieve by many ways but simplest is by creating a html div with some class or id and inside that div add image/video/gif of your explosion and set it to
display: none;
and whenever we want to show the explosion, set the display toblock/flex
as per your need.Below is working example. Your counter also working in negative values, I haven’t fixed it by thinking it might be intentional.
Assuming that the gif is inside a
<img>
element and has itsid
attribute set to "elementId", you can do the following to display the gif:Also, be sure to handle your timer, i.e, reset it or clear it. Have a look at this for more info.
To learn more about the
display
css property, visit: https://developer.mozilla.org/en-US/docs/Web/CSS/displayso you can use ClearInterval() and need to change some code.
You should create an element as overlay in HTMl with
position: fixed; inset: 0;
and hide it by default. You then can use in JSclassList.remove()
to remove the class that hides the overlay so it will be visible.You need to stop the interval if the time reaches 0 with
clearInterval
.I also refactored your code:
Refactor Informations
You should not use
on
events such asonload
. Those events can only exist once and they will overwrite each other. It might not seem important in your "little" project but it will teach you wrong habits. You should always useaddEventListener
to append events. That way you will not overwrite other events that might have been added. A common reason for an unintentional overwriting of an event is another script often written by other programmers.You should and do not need to hand over parameters all the time. The only place where this might happen is with the usage of a config file. In that case you should not use ladder logic but object-oriented programming using
Class
.With the ladder logic you used, you should have the time variable (actually a
let
not avar
) inside your discrete function. Same for the element which should be aconst
! However, referencing that element is not needed as you can call elements on their id directly in ES6.Do not write repetitive code such as:
Instead, write a discrete function.
This is hardly readable as it is easy to miss your decrementing. You write code not for machines but to be readable for other humans. So write longer code if it makes the code more readable and try not to hide simple things instead of writing them into a new line!
display
is a method in JS. it is not a suitable name for a variable to constant. Besides that, it is not an intuitive name for a timer. Always choose meaningful names that explain what the name is used for.please refer the above code