`The value of an array containing specific class elements is lost when accessed within a setTimeout

I've come across an issue while using a JavaScript file to hide several divs all sharing the same class name. The program successfully hides the divs, but when I try to make them visible again after a certain number of seconds, the array of elements becomes undefined with a length of 0. I'm not sure what's causing this problem. I attempted to change "lowerDash" to a global variable, but it didn't work.

function newBaseHandler(){

    if (document.getElementById("Base6").innerHTML == `<br><p>Create new base</p>`) {
        let lowerDash = document.getElementsByClassName("LowerDashboard");
        let message = document.getElementById("Message");
        for (let button of lowerDash) {
            button.style.visibility = "hidden";
        }

        message.innerHTML = `<br><p>Base created successfully</p>`;

        setTimeout(function() {
            message.innerHTML = ``;
            console.log(lowerDash.length);
            for (let button of lowerDash) {
                button.style.visibility = "visible";
            }
        }, 1000);

    }

}

Answer №1

To make them reappear after a delay, use the following code:

button.style.visibility = "visible"

inside your for loop within setTimeout function.

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

What is the reason behind JavaScript libraries opting for a structure of [{ }] when using JSON?

I have been experimenting with various Javascript libraries, and I've noticed that many of them require input in the format: [{"foo": "bar", "12": "true"}] As stated on json.org: Therefore, we are sending an object within an array. With this observ ...

What obstacles must be overcome when developing a CSS framework?

Currently, I am delving into the realm of popular CSS frameworks such as Bootstrap and Foundation. While studying them, I have identified aspects that I believe could be enhanced and customized to suit my own projects or potentially for free distribution i ...

Troubleshooting: Issue encountered while adding jQuery Slideshow plugin to current website

I am currently facing some challenges with a specific feature on my website. I am trying to integrate Jon Raasch's "Simple jQuery Slideshow Plugin" into an existing site that was not originally built by me. However, when viewing the site in IE 9, two ...

Arranging a variety of sophisticated charts side by side using Bokeh

Is there a way to display two or more Bar charts together in a single html file using Bokeh? I have found examples of laying out figures like triangle, square from bokeh.plotting together on this page: However, I have not been able to find any examples s ...

What is the best way to execute a second query in MySQL and PHP if the first query returns no results?

A dilemma I'm facing involves an html form passing values to a php file for query execution. The results are displayed, but now I want a scenario where if the initial query returns no rows (empty result), the same query should be executed on another t ...

Leveraging Unique Identifiers in Real-Time Namespace with SocketIO

I am in the process of developing a multi-tenant chat application and my goal is to have each tenant connect to their own namespace in socket io. I am using the unique identifier of the tenant, which is a UUID (for example: 5d056752-6643-4300-926f-5bcd5ed6 ...

Issues with VueJS rendering have been observed on MacOS with both Safari and Chrome browsers

Encountering an unusual issue here. My VueJS application, sourced from CDN, incorporates Vuetify. Interestingly, it functions seamlessly on Ubuntu and Windows; however, my client reports observing raw VueJS code when accessing the app via Safari on his iP ...

"Troubleshooting ways to resolve babel-eslint import/export issues without the need for a configuration

Upon running npm start, I encountered the following error message: Parsing error: 'import' and 'export' may only appear at the top level After investigating this issue, suggestions included updating the eslint configuration file with ...

The Simple HTML Dom parser is failing to parse contents on these specific websites

I tried using a basic HTML DOM parser but it's encountering issues when parsing certain websites. include_once 'simple_html_dom.php'; $html=file_get_html('http://www.lapenderiedechloe.com/'); This results in an error message like ...

How can I locate an element within the body of an if statement?

I am working on a simple JavaScript (jQuery library) if statement to check for the presence of a div element with the class 'video' on the page. If it finds the element, the variable 'arrows' is set to true, otherwise it is set to false ...

The convergence of lighttpd with AngularJS's html5 mode offers a powerful

My angularJS website is hosted on a lighttpd server, but the html5 mode encounters a 404 error when I refresh or use a direct link. On my local Apache server, everything works fine and there are no issues with the rewrite expression in the .htaccess file. ...

The auto-refresh feature of DataTables is not functioning as expected

Having trouble with the reload feature of DataTables. This is the code I'm using to load and reload the table on the server-side: $( document ).ready(function() { $('#dienst_tabelle').DataTable( { "ajax": "getData ...

Using identical CSS for two separate layouts

Imagine having to work with an html page that cannot be altered in any way. The only tool at your disposal is CSS. The page is loaded and shown through an iframe from a different domain. This particular page serves as a payment gateway, displaying either ...

Challenges with Loading JSON Dynamically in Next.js using an NPM Package

In my TypeScript project, I have implemented a functionality where a json configuration file is dynamically loaded based on an enum value passed as a parameter to the getInstance function in my PlatformConfigurationFactory file. public static async getIn ...

Using AJAX along with the append method to dynamically add identical HTML content multiple times to a single element

Although I have successfully implemented most of the desired functionality with this JavaScript code, there is a persistent bug that is causing unnecessary duplicates to be created when appending HTML. Detecting Multiples The problem lies in the fact tha ...

How do you modify the SVG viewport using code?

I am looking to create a feature that allows all images inside an SVG object to be moved. My plan is to use JavaScript, and possibly jQuery, to handle mouse events (down, move, up) in order to change the viewport of the SVG. However, I am currently facing ...

Tips for ensuring that an Ajax request successfully executes when a page loads

I have a question about implementing an AJAX request in my code. Currently, I have the text on the screen updating when a dropdown input is selected using 'onchange'. However, I also want this same behavior to occur on page load, but I am struggl ...

Sending arguments to browser.executeScript

How can I provide parameters to browser.executeScript static sortableDragAndDropByClassName(dragElementClassName: string, dropElementClassName: string) { return browser.executeScript(function () { console.log(dragElementClassName); conso ...

How can you smoothly adjust the orientation of an object to align with a specific point

I am currently developing a javascript game where I need enemy ships to rotate towards a specific point in order to calculate their movement based on the angle. However, I am facing an issue with the rotation code as it only works properly when the ship is ...

An operator in rxjs that transforms an Observable of lists containing type X into an Observable of type X

Currently, I am facing a challenge while dealing with an API that is not very user-friendly using Angular HTTP and rxjs. My goal is to retrieve an Observable<List<Object>> from my service. Here's a snippet of the JSON output obtained from ...