"Set a timeout for an HTML markup to be displayed in an AJAX

Is there a way to automatically hide the success message that appears after an AJAX request is successful? For example, after 2 seconds of displaying the message, I want it to disappear.

Here's the code I have:

$.ajax({
    url : 'process/register/registerDB.php',
    success : function (data, textStatus, jqXHR) {
      timer = setTimeout(successNotification, 2000);
    }
});

function successNotification {
  $('#alert-box')
    .html('<div class="alert alert-success" role="alert">User <b>'
          + newUserArr["fullname"]
          + '</b> Successfully Submitted</div>');
}

What could be the issue with my implementation?

Answer №1

function fetchData() {
    $.ajax({
        // include your parameters here
    }).done(function (result) {
        // perform actions with the returned data
    }).always(function () {
        window.setTimeout(fetchChatData, 1101);
    });
}
fetchData();

The purpose of using .always is to prevent any errors while retrieving messages.

Answer №2

When working with Ajax, consider using setTimeout in a slightly different way:

setTimeout(function () { displaySuccessMessage(); }, 2000);

Give this method a try, it could be beneficial for your project

Answer №3

Make sure to update timer = setTimeout(successNotification, 2000); to timer = setTimeout("successNotification", 2000);

The only difference is adding quotes around the function name. The successNotification message will now be triggered after a 2-second delay.

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 method to retrieve data in Vue when utilizing arrow functions?

When utilizing Vue and arrow functions in methods, we encounter the issue that arrow functions do not have a this keyword. The question then arises of how to access data properties such as list or object. export default { data() { return { list ...

jQuery Slider Showing Incorrect Images

My jQuery slider is acting strangely. It hides the first image, shows the second one, but then just repeats the cycle by showing the first image again instead of loading the third one. I'm puzzled about what could be causing this issue in my code. Do ...

Explore the differences between user input and JavaScript

There seems to be an issue with the second output result. var compareNumber = 3; // Code will be tested with: 3, 8, 42 var userNumber = '3'; // Code will be tested with: '3' 8, 'Hi' /* Enter your answer here*/ if (userNum ...

Is there a way to retrieve the timezone based on a province or state in TypeScript on the frontend?

I've been working on an angular app that allows users to select a country and state/province. My current challenge is determining the timezone based on the selected state/province, with a focus on Canada and the US for now (expanding to other countrie ...

In Protractor, mastering the technique to extract multiple values simultaneously is crucial for efficiently handling applications that receive a large amount of push notifications

I am currently developing an automation test using Protractor for an application that receives a large volume of push notifications. The issue I am facing is testing a simple logic. expect(A + B).toEqual(C); The problem arises because A, B, and C are sou ...

What is the best way to retrieve a specific object from a JSON file using a Get request in a Node.js application?

My focus is on optimizing an API, which is why I'm working with only the data that's essential for my analysis. I've set up a route to extract specific objects, but I'm only interested in four of them: account_manager, fronter, closer, ...

Utilizing Bootstrap Popover in Grails with the power of AJAX

Exploring the utilization of ajax formRemote within a Bootstrap popover. While a basic form functions properly, the ajax implementation does not: 1) GSP Code <div id="addfolder"> <a href="#" class="btn" id="nrfolder" rel="popover"& ...

Creating a Dynamic Form with jQuery, AJAX, PHP, and MySQL for Multiple Input Fields

Success! The code is now functional. <form name="registration" id="registration" action="" method="post"> <div id="reg_names"> <div class="control-group"> <label>Name</label> <div class= ...

Exploring the Usage of sessionStorage within the <template> Tag in Vue.js

Is it possible to access sessionStorage in the script section of a Vuejs component like this? <template> {sessionStorage} </template> Whenever I try to call it this way, I consistently receive the error message "cannot read property &apo ...

Encountered a failure while loading modules in AngularJS

When I tried opening the index.html page using Chrome, I encountered an error stating that the correct modules could not be found. Uncaught SyntaxError: Unexpected token < angular.js:1 Uncaught SyntaxError: Unexpected token < controller.js:1 ...

The constructor for audio in Next JS cannot be found

I'm facing an issue and struggling to find a solution. My project follows the standard structure of Next JS. In the root directory, I have included a components folder. Within the components folder, there is a component with the following code: imp ...

Update settings when starting with chromedriver

I am currently using webdriver (), standalone selenium, and mocha for writing my test cases. These test cases are specifically designed for Chrome, so I rely on chromedriver for execution. However, when launching the browser, I need to ensure that the "to ...

Having trouble making an AJAX request work in AngularJS?

Just dipped my toes into the world of Angular (only a few hours in). Managed to tweak the demo to get close to what I need, but hitting a roadblock with my AJAX request. After trying a variety of fixes, one puts me in an endless loop (discovered that&apos ...

JMeter encountered a 500 Internal server error while trying to call the API

Hey everyone, has anyone encountered a 500 internal server error when trying to call an API? I have developed an application that retrieves data from an API. I am currently conducting performance testing on this functionality using JMeter. To perform the ...

not getting any notifications from PHP

Despite receiving a status of '1' from this process file, my JavaScript code seems to be working fine. However, I am facing an issue with not receiving the email. <?php //Retrieve form data. //GET - user submitted data using AJAX //POST - in ...

ReactJS Modal popping up repeatedly while inside a loop

Hey there, I've been experimenting with ReactJS and came across this amazing Modal Component for opening videos in a modal. However, I encountered an issue when placing the Modal inside a loop with multiple links - it opens multiple times correspondin ...

Assistance needed for jQuery functions running too early

Within my click function, I am setting certain flags and then calling two functions. However, I want these two functions to execute sequentially in order after the flags have been set. jQuery("#element").click(function(e){ var op = jQuery("#box" ...

The hash navigation feature of jQuery Mobile fails to function in a multipage template when a user enters the site through an interior page

Site Navigation Issue After setting up a website with jQuery Mobile, I encountered a problem. When entering the site from an interior page like the blog and clicking on navigation buttons in the header to return to the homepage, none of the ajax or hash-b ...

Move the cursor and watch as the Hover Image seamlessly follows its path

Currently, I am attempting to create an interactive hover effect with an image that moves along with the mouse cursor. While I have come across various resources for achieving this effect, the limitation of working within a Wordpress theme restricts my ab ...

React does not auto-populate form data

I am working on a simple project using Next.js with a basic form. However, when I try to submit the form and log the form data in the console, it shows up as an empty object. I'm puzzled by this issue. Here is my code: import styles from &apos ...