What is the process for creating a blinking, overlapping image using Javascript?

I'm trying to achieve a visually appealing effect with this countdown. The goal is to have it count into negative numbers, at which point a skull and crossbones image should become visible and start flashing. To do this, I am utilizing JavaScript functionality...

var img = document.getElementById('Image1');
var interval = window.setInterval(function() {
    if (img.display == 'hidden'){
        img.style.visibility = 'visible';
    } else {
        img.style.visibility = 'hidden';
    }
}, 1000);

Once the flashing effect is in place, what is the best way to make sure it overlaps the countdown span? http://jsfiddle.net/2Lufxs2t/3/

Answer №1

  1. Following the line

    var img = document.getElementById('Image1');
    , include
    document.getElementById('countdown').style.display='none';
    to conceal your timer.

  2. Eliminate the second interval in your code:

    var interval = window.setInterval(function(){
    . Keep only the if statement that controls visibility. The issue lies in the fact that secondPassed() is running every second, and then your second interval is also being executed every second. This results in the inner interval running twice per second and toggling itself repeatedly.

Answer №2

JSFiddle This solution worked perfectly for my needs.

<html>
    <head>
    <title>Timer</title>
    <script type="text/javascript">
    var seconds = 10;

function stopTimer() {
    clearTimeout(countdownTimer)
}

function floor(x) {
    return x | 0;
}

function pad(n) {
    if (n < 0) {
        n = -n;
    }
    if (n < 10) {
        return '0' + n.toString();
    }
    return n.toString();
}

function secondPassed() {
    var minutes = pad(floor(seconds / 60));
    if (seconds < 0) {
        minutes = '-' + minutes;
    }
    var remainingSeconds = pad(seconds % 60);

    document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
    if (seconds > 0) {
        seconds--;
        if (seconds > 8) {
            document.body.style.backgroundColor = "green";
        } else if (seconds == 5) {
            document.body.style.backgroundColor = "yellow";
        } else if (minutes == 0 & seconds == 0) {
            document.body.style.backgroundColor = "red";

            if (seconds%2 == 0) {
            document.getElementById('skull').style.display="none";  
            } 

            if (seconds%2 != 0) {
            document.getElementById('skull').style.display="block";             
            }


        }
    } else {

                if (seconds%2 == 0) {
            document.getElementById('skull').style.display="block";  
            } 

            if (seconds%2 != 0) {
            document.getElementById('skull').style.display="none";              
            }

        seconds--;
    }
}
    </script>
    </head>
    <body>
     <img id="skull" src="http://s15.postimg.org/es5w3xpob/skull.gif"style="position:absolute;  z-index: -1;display:none;">
     <div style=" z-index:10;">
    <p align ="center">
    <span id="countdown" style="color:black; font-size: 450px; font-weight: bold;" ></span>
    </br>
    <button onclick="countdownTimer = setInterval('secondPassed()', 1000)">Start</button>
    <button onclick="stopTimer()">Stop</button>



        </p>

        </div>
    </body>
    </html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to update a Rails element using an AJAX request?

I've delved into a plethora of information regarding Rails, AJAX, and 5.1 Unobtrusive Javascript. It provides insight on how to handle AJAX calls in Rails with a .js file, for example. However, my goal isn't to serve up an entire .js file; rathe ...

Is it possible that the div's height is not being set to 0 pixels

<div style="height:0px;max-height:0px"> </div> It appears that setting the height of a div to 0 pixels is not having the desired effect. The div is still expanding to display its contents, so how can we stop this from occurring? ...

What are the reasons for validation failure when it is moved into a method?

I am currently in the process of developing a validation function in JavaScript. However, when I extracted the logic into a private method, my validation started failing and I can't seem to figure out why. Here is my HTML definition: <input type= ...

`Something to keep in mind: Chartist in jspm does not import CSS files`

I'm running into some issues trying to import the chartist library with my jspm Aurelia application. I've added all the necessary styles, but the chart is not displaying as expected in terms of colors and lines. Here's the code snippet incl ...

What is the best way to retrieve the last 3 elements that have a specific name value using JMESPATH and JavaScript?

Below is a JSON data array that I have: [{"name":"Button1","date":"1596959802144"},{"name":"Button2","date":"1596959804238"},{"name":"Button3","date":"1596959809334"},{"name":"Button1","date":"1597000878135"},{"name":"Button2","date":"1597000896335"},{"nam ...

Implementing Default Language in Next.js 14 for Static Export without URL Prefix: A Step-by-Step Guide

Currently, I am in the process of developing a website using Next.js 14, with the intention of exporting it as a static site for distribution through a CDN (Cloudflare Pages). The website I am working on requires support for internationalization (i18n) to ...

Showing user data in a div using JavaScript ajax without the use of jQuery

Recently diving into the world of javascript and ajax, I find myself tinkering with code on jsfiddle. My current goal is to populate a list with usernames and emails upon form submission. Upon submitting the form, an error message pops up: {"error":"key m ...

Type parameter in express.js route

If I have this specific route in my Express.js server: router.get('/ad/:id', (req, res) => { const { id } = req.params Ad.getAd(id, (err, resp) => { if(err){ return handleError('Failed to load an ad' ...

What is the reason for replace() not working with the word "my"?

Is there a reason why the replace() function in the code snippet below does not replace "my"? $(function() { var str = "put image in my gallery"; str = str.replace(/ my | in /g, " "); }); You can find the code on jsfiddle here. Appreciate any insigh ...

Retrieving the checkbox value from a dropdown selection

I'm stuck and feeling lost here - I must be missing something obvious. Any help will be greatly appreciated! (I am a beginner in html and javascript) I have created a dropdown menu with an unordered list of items populated from JSON data. Here is the ...

Tips for utilizing the simple-peer module within a Node.js environment?

I recently started using Node.js and I'm interested in incorporating the simple-peer module into my application. However, I am struggling to understand how to implement it based on the provided documentation. Are there any resources available that can ...

The CSS grid-template-areas feature is not performing as anticipated

.content { width: 600px; height: 600px; border: 4px solid lime; display: grid; grid-template-areas: 'a d' 'b d' 'c d'; } textarea, iframe { margin: 0; box-sizing: border-box; } .content > .a {gri ...

Issue with ng-bind not functioning correctly within a custom directive on a label

A custom directive was implemented to incorporate a hint-icon with a description for labels. Here is the code snippet for the directive: propertyWindowModule.directive('hintIcon', { restrict: 'A', controller: HintI ...

The div just won't stay the size I want, no matter what

Here is a snippet of HTML code: <div class="pointwrap"> <div class="imgwrap"> <img src="howtoplay1.jpg" height="101" width="177"/> </div> <div class="textwrap"> Tap Anywhere in the Drop zone to release a ball. ...

Scatter chart in highcharts showcasing data in 4 quadrants

Disclaimer: I may be new to utilizing chart frameworks and my JavaScript skills might not be top-notch. I attempted to create a scatter chart using highcharts, which turned out to be quite messy. You can view it here. I need to modify it so that it has 4 ...

What is the best way to show the current value of a checkbox element in the future?

I want to add multiple checkboxes using this method: $(".btn-sample").on("click", function() { $(".container").append( '<input class="check-sample" type="checkbox" value="'+check_value+'"/>' ); }); The issue I' ...

In HTML, a Bootstrap row appears on top of another row when viewing on mobile devices

I have implemented a Bootstrap HTML section that consists of a container and two main rows. Here is the code snippet: <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <style& ...

Issue with Bootstrap 4 navbar functionality on smaller screens

I'm currently in the process of integrating a navbar that transforms the navbar choices into a dropdown menu on smaller screens (as illustrated in the documentation available here). However, I am encountering an issue where the button responsible for ...

The usage of $http.post in combination with TypeScript accessors

Currently, I am developing an AngularJS application (v1.3.15) using TypeScript 1.5 in Visual Studio 2013. I have encountered a challenge related to TypeScript object properties and JSON serialization while utilizing $http.post(). Although I consider myself ...

Tips on extracting information from Chrome's network console feature

I took a screenshot from Chrome's developer console After examining the image, I'm wondering if it's possible to extract data using javascript/Node.js? I've already checked out Firefox's documentation for performance.getEntries() ...