How can I display a DIV message every time "X" minutes have passed?

View my example on JSFiddle for a visual demonstration. My goal is to have a message appear in the top left corner every 5 minutes, flicker a few times, and then fade away gradually. I'm not very skilled in CSS and was thinking it may require some JavaScript as well. The message should be enclosed in a rounded-corner box. Below you'll find my CSS code.

Thank you in advance!

HTML:

  <div id="container">
    <div id="map_size" class="map_size">   
      <div id="msg">
        New records available.          
      </div>    
    </div>
  </div>

CSS:

/*body*/
body{
  margin:0px auto;
  width:80%;
  height:80%;
  font-family: Verdana,Geneva,sans-serif;
}

/*container for all divs inside*/
#container {
  width:1450px;   
}

/*map size*/
#map_size{
  width:1190px;
  height:1300px;
  background:#0099FF;
  border-style: solid;
  border-color: black;
  position: relative;
  float:left;
}

/*msg*/
.station_info_ {
  z-index: 100;
  position: absolute;
  background-color:white;
  border-radius: 5px;
  height: 200px;
  margin-left: 50px;
  width:  275px;
  border:4px solid black;
}

JS: I want the message to be hidden when the page loads, and only show every 5 minutes after that. How can this be achieved?

<script type="text/javascript">
     $('document').ready(function(){
      window.setInerval('test()',3000);
     });

    function test(){
     $('#msg').toggle();
    } 
 </script>

Answer №1

To achieve this, you can use the setInterval() method after the completion of setTimeout().

Here is a sample of working code:

(I have set the timing to 3 seconds for quick testing, but you can adjust it to any desired time interval.)

$('document').ready(function(){
    
    $('#msg').hide();
    
    window.setTimeout(
        function(){
            $('#msg').show();
            setInterval(function(){ $('#msg').toggle(); }, 3000);
        }        
        ,3000);
});
body{
    margin:0px auto;
    width:80%;
    height:80%;
    font-family: Verdana,Geneva,sans-serif;
}

/*container for all divs inside*/
#container {
    width:1450px;   
}

/*map size*/
#map_size{
    width:1190px;
    height:1300px;
    background:#0099FF;
    border-style: solid;
    border-color: black;
    position: relative;
    float:left;
}

/*msg*/
.station_info_ {
    z-index: 100;
    position: absolute;
    background-color:white;
    border-radius: 5px;
    height: 200px;
    margin-left: 50px;
    width:  275px;
    border:4px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    <div id="map_size" class="map_size">   
        <div id="msg">
            new records available.          
        </div>    
    </div>
</div>

Check out the demo on jsFiddle

Answer №2

To implement a countdown timer in JavaScript, you can use the

setInterval(function(){ alert("After 3000 milliseconds"); }, 3000);

For more information about the setInterval method, check out this resource from MDN:

The setInterval() function is used to call a specified function or execute a code snippet at fixed time intervals. It returns an intervalID.

Additional Notes:

  1. Create a custom popup window (e.g., "Search Google") and show a message within it.
  2. After displaying the message, you can use the .hide() method to hide the popup window.

Note that with JavaScript, you cannot programmatically close an alert box until the user interacts with it.

Answer №3

If you're looking to incorporate some fun animations, consider using animate.css as shown in the demo below. While jQuery animations can also be used, animate.css is simpler and offers a wide variety of animation styles to experiment with.

All you need to do is switch out the animation names to customize your effects.

For a list of available animation styles, visit the animate.css website.

Hari Krishnan's tip mentioned here works like a charm (it's utilized in the demo).

The line

$msg.show().removeClass().toggleClass(flag ? inAnimation : outAnimation);
may seem complex. It essentially displays the div and applies the CSS class stored in inAnimation. As the flag turns false in the next loop, the CSS class from outAnimation is triggered. The removeClass() ensures that no extra styles interfere with the animation CSS. (There isn't a hide() function since the CSS fadeOut feature automatically hides the element).

You can run the demo on this jsFiddle link.

$('document').ready(function () {
    var $msg = $('#msg');
    var displayTime = 0.1; // in minutes 0.1 = 6 seconds / 1 = 1 min.
    var flag = true;
    
    $msg.hide();

    setInterval(

    function () {
        var inAnimation = 'fadeIn animated';
        var outAnimation = 'fadeOut animated';
        //console.log(flag);
        $msg.show().removeClass().toggleClass(flag ? inAnimation : outAnimation);
        flag ^= true;
    }, displayTime * 1000 * 60);
});
body {
    margin:0px auto;
    width:80%;
    height:80%;
    font-family: Verdana, Geneva, sans-serif;
}
/*container for all divs inside*/
 #container {
    width:1450px;
}
/*map size*/
 #map_size {
    width:1190px;
    height:1300px;
    background:#0099FF;
    border-style: solid;
    border-color: black;
    position: relative;
    float:left;
}
/*msg*/
 .station_info_ {
    z-index: 100;
    position: absolute;
    background-color:white;
    border-radius: 5px;
    height: 200px;
    margin-left: 50px;
    width: 275px;
    border:4px solid black;
}
<link href="http://cdn.jsdelivr.net/animatecss/3.2.0/animate.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
    <div id="map_size" class="map_size">
        <div id="msg">new records available.</div>
    </div>
</div>

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

Send a file using ajax with the help of JavaScript and PHP

Currently, I am looking to implement a method for uploading files using Ajax and JavaScript/PHP without having the page refresh. My initial thought is to use Ajax to send the file using xmlhttp.send(file) and then retrieve it in the PHP script, but I' ...

What are the steps to showcase StreetViewPanorama within a React application?

Is it possible to have a fully working streetview using google API key? I've come across some libraries online, but their documentation seems poor or outdated. I attempted to use the @react-google-maps/api library with documentation available at . Ho ...

How can I modify the ngx-datatable pager component to display text instead of icons and include a totalVisible property?

I am currently customizing the datatable-pager in ngx-dataTable and have been successful in adding footers and pagers. However, I am facing two issues that need resolution: How can I display text instead of icons for the prev/Next/First and Last buttons? ...

What is the proper method for utilizing $http and $q in an Angular service?

I am currently working on implementing reverse geocoding to retrieve the city name from latitude and longitude data. After developing an Angular service that interacts with the Google Maps API using $http, I found myself facing challenges due to the async ...

The token endpoint in Nuxtjs auth module's configuration for auth strategies is not being triggered

Our system has two important endpoints, namely /auth and /token. The endpoint /auth is responsible for providing the authorization code required to call /token in order to obtain an access token. The utilization of NuxtJS has made the auth module a prefer ...

Is it possible to update only the necessary data using the Update Controller?

Update Controller exports.UpdatePanelMembers = (req, res) => { const { panelId } = req.params; if (panelId) { Panel.findOneAndUpdate( { _id: panelId }, { panelMembers: { member1: { memberId: req.body.pan ...

How can I place a new ThreeJS child element at the front and center of a scene?

I have been working on a webpage that is inspired by the CSS3D molecules sample from ThreeJS's library. You can check out the original sample here. In my project, I am dynamically creating new nodes (atoms) and attaching them to existing nodes. Once ...

What steps should I take to ensure my Bootstrap CSS3 code is error-free and W3C validated?

My customer is requesting that I validate the HTML5 & CSS3 code using W3. I plan on incorporating Bootstrap 3.3.4 into the project, but after including it and checking at , I discovered 32 errors and 142 warnings. The W3C CSS Validator results for (C ...

Stacked text appearance in pseudo CSS/HTML5 styling

Trying to create a website for a charity but the text is unexpectedly stacked on the page. I attempted to use CSS3 and HTML5, although not perfectly compliant. After researching how to stack with css3, it appears that none of those techniques were inadver ...

Display subcomponents from an array when the state is updated

In my React parent component, I am utilizing graphql to fetch JSON data and pass it down as a prop to the child components. My goal is to use state to update the page when one of three objects in the JSON file is clicked. class Work extends React.Componen ...

JavaScript Function for Dropdown Select Box

Here is a menu for selection: <select> <option value="0" onclick="anders('1')">Anders</option> <option value="200" onclick="anders('');" selected="selected">&#8364; 200,-</option> <option value="300 ...

The default file type for a new file in Sublime Text 2

While searching through the questions and answers, I noticed that none of them quite matched what I was looking for. Each time I open a new file, it automatically defaults to a plain text file. Since I primarily work with HTML files, I was wondering if the ...

Automatically bookmark webpages using JavaScript in your browser

I am hoping to trigger the add to bookmark action upon page load using JavaScript or jQuery. ...

Encountering an issue with the node.js express server when fetching data

I'm running into an issue with the fetch function and node.js. When a button is clicked on my frontend, I want to send a post request to receive an array from my backend as a response. My backend is built using node.js with express, and I'm using ...

executing tasks on a sql database with php

Currently, I am delving into the realm of SQL and have devised a table named products. This table consists of columns such as item, price, base, and wholesale. My goal is to enable users to input a number (let's say 15) which signifies their desire t ...

Unlocking the Power of Django: Transforming Your Created Topics into Publicly Accessible Content in the Learning Log Project

The Challenge I'm currently working on a project that involves creating topics and entries, some of which can be private or public to unauthorized users. Each topic can have multiple related entries. The issue I'm facing is with implementing a c ...

Animating path "d" with ReactJS and SVG upon clicking in FireFox

Visit this CodePen for more: https://codepen.io/sadpandas/pen/xxbpKvz const [currentScreen, setCurrentScreen] = React.useState(0); return ( <React.Fragment> <div> <svg className="theSvg" width="156" height="6 ...

How can I utilize jQuery to create a remove button that deletes individual div elements one at a time?

<div class= "button"> <button id="More" type="submit">Click here to add more Parameters</button> <button id="Remove" type="submit">Click here to Remove Parameters</button> </div> <script> var count = 2; ...

Displaying various results using a range slider

I really need some assistance in making this range slider produce multiple outputs. I've attempted a few different options, but unfortunately, I haven't been able to figure it out. My goal is to have the text "590" display as 5.9 times the value ...

Is there a more concise method to locate an object key within an array of objects?

Unfortunately, I am unable to alter the data that is provided to me. As a result, I have come up with a workaround, although it feels a bit messy. Let's take a look at an example of the data I am dealing with: var x = { // a simple array, which w ...