Delete items within the first 10 minutes of shutting it down

Is there a way to temporarily remove a newsletter element for 10 minutes after closing it on a webpage? The idea is that once the panel is closed, it should stay hidden even if the page is refreshed within that timeframe. I was considering using local storage, but it seems local storage doesn't offer an expiration feature. Another thought was to utilize setTimeout, but I'm not certain how to implement it in this specific scenario. It's important that this functionality is achieved solely through JavaScript without relying on any external libraries or frameworks. You can find a working example on JSFiddle.

<section class="newsletter">
  <div id="newsletter_container" class="newsletter_class"gt;
    <h1>Panel Title
      <button id="close_btn">close</button>
    </h1>
    <h3>Panel content</h3>
  </div>
</section>

To achieve the desired effect, we can make use of CSS animations along with JavaScript event listeners. Below you will find relevant code snippets for styling, animation, and interaction:

.newsletter {
    position: fixed;
    bottom: 0;
}



@keyframes Slide_down {
    0% { transform: translateY(0); }
    100% { transform: translateY(250px); }
}

#newsletter_container {
    display: block;
    background-color: rgb(0, 122, 193, 0.7);
    width: 500px;
}
// More CSS styles...

const newsletter = document.getElementById("newsletter_container");
const closeBtn = document.getElementById("close_btn");

closeBtn.addEventListener("click", () => {
    newsletter.classList.add("slide_down");
    newsletter.addEventListener("webkitAnimationEnd", function() {
        newsletter.remove();
    });
});

If you encounter issues with elements reappearing after reloading, consider implementing a solution using localStorage as demonstrated here: JSFiddle. Here's a snippet from the implementation:

let removeNewsletter = localStorage.getItem('removeNewsletter');

const closeNewsLetter = () => {
    newsletter.classList.add("close");
    localStorage.setItem('removeNewsletter', 'enabled');
}
// Additional scripting...

Answer №1

To retrieve the starting time using JavaScript, utilize the Date.now() function and store it locally. When the page is loaded again, obtain another timestamp with Date.now(), then compare it to the stored time when the page refreshes. If the elapsed time exceeds 10 minutes, take necessary action.

You can perform the comparison as follows:

var storedTime = Number(localStorage.getItem("storedTime"));
if (storedTime){
var difference = (Date.now() - storedTime) / 60000;

if (difference < 10){
    newsletter.style.display = "none";
}
}

Note that the time from Date.now() is in milliseconds, hence dividing by 60000 converts it into minutes.

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

JavaScript function to copy the first row of a table to the clipboard

I am new to JavaScript and I'm facing an issue with copying cells in a table. I have successfully created a table based on my MySQL database, but when I try to copy a cell using the copy button, only the first cell gets copied. I understand that I nee ...

The bootstrap switch button remains in a neutral grey color

Ordinary: Initially clicked: Click again: After the second click, it remains grey and only reverts on a different action like mouse click or selection. Is there a way to make it go back to its original state when unselected? ...

Is there any npm module available that can generate user-friendly unique identifiers?

Struggling to come across a user-friendly and easily readable unique ID generator for storing orders in my backend system. I have considered using the timestamp method, but it seems too lengthy based on my research. ...

Searching for a JavaScript tool that offers syntax highlighting capabilities

I have come across some excellent RTE HTML text editors such as jsredaktor, TinyMCE, and FCK Editor, but I am in search of an HTML/JavaScript component that functions similarly to a TEXTAREA with built-in code syntax highlighting. ...

Continue running the ajax request repeatedly until it successfully retrieves results

At the moment, I am using a basic ajax call to retrieve data from our query service api. Unfortunately, this api is not very reliable and sometimes returns an empty result set. That's why I want to keep retrying the ajax call until there are results ( ...

Is there a better way to implement an inArray function in JavaScript than using join and match?

Recently, I created an inArray function for JavaScript which seems to be working fine. It's short and a bit unusual, but I have a nagging feeling that there might be something wrong with it, although I can't quite pinpoint what it is: Array.prot ...

leveraging an array from a separate JavaScript file within a Next.js page

I am facing a situation where I need to utilize an array from another page within my Next.js project. However, it seems that the information in the array takes time to load, resulting in encountering undefined initially when trying to access it for title a ...

Struggling to accurately differentiate between a CSS and Bootstrap code

I'm attempting to create a visually appealing underline under the active tab, but I am facing challenges in targeting the specific class. Since I am utilizing bootstrap and unable to adjust the width and margin of text-decoration or recognize an eleme ...

The picture is not visible outside the parent container - why is it hidden?

Currently, I am working on project cards and using materialize css's image card to style them. One of the features that I particularly like is how it resizes the picture when included inside a container. This allows me to set a fixed height without wo ...

Encountering issues with the phaser framework while setting up a server on xampp, faced with errors in javascript and html

Recently, I've been working on a game using Phaser and encountered some issues while trying to run the code in XAMPP. The game's base is located at htdocs/itw/test.html, with the necessary pngs and json file stored in htdocs/itw/assets/. The pngs ...

Programmatically toggle the visibility of an ion fab button

Looking for assistance in finding a method to toggle the visibility of a particular button within the collection of buttons in an ion-fab ...

How can I apply concatMap in Angular?

Can you please guide me on how to effectively utilize concatMap with getPrices() and getDetails()? export class HistoricalPricesComponent implements OnInit, OnDestroy { private unsubscribe$ = new Subject < void > (); infoTitle ...

You can activate the ability to click on the main tag by setting routerlink as the child component in Vue

Within my HTML code, I have utilized a RouterLink within an li tag to create a basic dropdown menu. Currently, when options are displayed in the dropdown menu, I am only able to click on the text itself to navigate to the next page. However, I would like t ...

Is the Wordpress search bar a clickable link?

Whenever I attempt to click on the search box, it redirects me to the comments section of a post as if it were a link. It's difficult to provide more information about this issue, but the problem occurs on the website lbk.newcoastmedia.com/wordpress w ...

Can Selenium in JavaScript be used to retrieve child elements from a webpage?

I've been struggling to adapt my JavaScript script for use with Selenium (also in JavaScript). Despite numerous attempts, I haven't been able to find a solution. I've attached an image to better explain my question await chromeDriver.findEle ...

What is the procedure for employing suitscript within Restlet in the NetSuite environment?

Currently, I am working on creating a restlet in Netsuite to retrieve details about a specific Field. The code snippet I have been using is as follows: error code: UNEXPECTED_ERROR error message:TypeError: Cannot call method "getType" of null (login.js$12 ...

Incorporating React.js with a server API that doesn't use JavaScript

My upcoming project involves enhancing my current application frontend by implementing React.js. The existing frontend currently utilizes standard REST calls to communicate with a server built using Microsoft technologies, and there are no plans of changin ...

Is there a way to eliminate duplicates from an array in JavaScript by utilizing a set and placing each element in an li tag? Here is the code

const numbers = [" 4", "9", "16"," 4", "9", "16"," 4", "9", "16"] <ul> {(<li> {[...new Set(numbers)]} </li>)} </ul> const numbers = [" 4", "9", "16"," 4", "9", "16"," ...

Unable to retrieve the second number enclosed in parentheses using regular expressions

Currently, I am diving into the world of regex and encountering a set of strings formatted as "(9/13)". My goal is to retrieve the second number in this format. To achieve this, I experimented with the following regex pattern: /\(.*?[^\d]*(\ ...

What is the most effective approach to seamlessly conceal and reveal a button with the assistance

I have two buttons, one for play and one for pause. <td> <?php if($service['ue_status'] == "RUNNING"){ $hideMe = 'd-none'; } ?> <a href="#" class="btn btn-warning ...