Avoid the scenario in which JQuery .HTML replaces the existing .HTML content before it is shown

In my current implementation, I am using a multidimensional array to store values and loop through it based on specific criteria. This process involves determining if a number matches and if it has a set status, such as "487 Online."

The data in the array is updated in real-time based on server-side events, and everything runs smoothly on that end.

My goal is to visually represent each state of a call in the following manner:

  1. If the phone device is online, turn the tile red; otherwise, turn it green and apply .HTML.
  2. If the phone is ringing, turn the tile yellow and apply .HTML.
  3. If the phone is in a call, turn the tile orange and apply .HTML.
  4. If the phone just hangs up, apply .HTML (which doesn't seem to work) and then turn blue using a CSS class with .addClass......after 3 seconds, remove the class (it doesn't display because the online status is captured right after, causing the blue color to be missed; hence, a setTimeout function is used to address this).

The first three steps work as intended, but the issue arises with the fourth step.

The problem I am encountering is related to using JQuery .HTML to modify the content in the tile every time the phone changes state. After debugging and experimenting, it seems that the issue is with how I am using JQuery .HTML and/or the setTimeout function, though I am unsure of the exact reason.

The issue: The Hangup event is triggered, but the .HTML content here doesn't display; instead, it appears to be overwritten by the .HTML from the online event, which is captured immediately after the hangup event. This results in the online event's .HTML being displayed instead.

I have noticed that removing the .HTML in step 1 resolves the issue, but I need it for that step. However, if I leave it there, it seems to overwrite the .HTML in step 4, which is also necessary for some reason.

Here is an example of how the array looks with the extension number, SIP device status, and current state of the device (e.g., Ringing):

  [ '487', 'online', 'Hangup' ],
  [ '488', 'online' ],
  [ '477', 'online', 'Hangup' ] ]

At the server-side, the events generally look like this when the event is triggered:

477 and 487 both hungup
[ [ '487', 'online', 'Hangup' ], [ '477', 'online', 'Hangup' ] ]
something happened to a ChannelDestroyed
Channel Output ChannelDestroyed
something happened to an endpoint
EndpointStateChange
477 current state is: online
[ [ '487', 'online', 'Hangup' ], [ '477', 'online', 'Hangup' ] ]

Below is the current code I am using:

// Code to handle events signaling current call states
socket.on("eventsCalls", function (calldata) {
    // Loop through the data and apply appropriate styling
    for (var i = 0; i < calldata.length; i++) {
        if (calldata[i][0] && calldata[i][2] === "Ringing") {
            // Implement actions for ringing state
        } else if (calldata[i][0] && calldata[i][2] === "Hangup") {
            // Implement actions for hangup state
        } else if (calldata[i][0] && calldata[i][2] === "ANSWER") {
            // Implement actions for answer state
        }
    }
});

// Code to handle registered sip devices
socket.on("eventsRegister", function (regisdata) {
    // Loop through the data and apply appropriate styling
    for (var i = 0; i < regisdata.length; i++) {
        if (regisdata[i][0] && regisdata[i][1] === "online") {
            // Implement actions for online state
        } else if (regisdata[i][0] && regisdata[i][1] === "offline") {
            // Implement actions for offline state
        }
    }
});

Are there any better alternatives or workarounds to address the issue I am currently facing?

EDIT: JSFiddle provides an idea of what I am trying to achieve. It is challenging to replicate the event catching, but when the hangup event occurs, the .HTML content appears to be overwritten by the online event, which might happen simultaneously. This behavior differs from the last two events in my code, where the colors change but the .HTML for hangup seems to be replaced by the online event.

Answer №1

It's possible that there is an issue with the setTimeout function in your code. Specifically, it seems like the delay parameter is missing.

var hangupDelay = 5000; // 5 seconds 
setTimeout(function () {
                $("div[class*='tile']").removeClass("hangup");
            }, hangupDelay );

The intention here is to remove the hangup class after a delay of 5 seconds. However, without specifying the delay parameter in the setTimeout function, the action may happen almost instantly.

If you're unsure about how the delay parameter works, you can refer to this resource for more information: https://developer.mozilla.org/en/window.setTimeout#Minimum_delay_and_timeout_nesting

The delay parameter represents the number of milliseconds that the function call should be delayed by. If not provided, it defaults to 0. It's important to note that the actual delay may end up being longer than expected.

This issue could potentially result in the hangup class being removed instantly, giving the impression that it's not working properly.

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

Elegant Popup Form

I'm encountering a problem with my ajax form that is embedded within a bootstrap modal. On the first use, the form functions correctly. However, when I attempt to use it again without refreshing the page, the text area no longer appears within the mod ...

Ways to release a client-side script with npm?

Within my nodejs package, I have included code that can be executed on both the backend and in a single .js file for browsers. In order to utilize the browser script, it must be placed within a script element in an HTML file. My query pertains to whether t ...

Learn how to retrieve data from a JSON server in Angular 8 and then sort that data in a table by utilizing checkboxes

Currently, I'm in the middle of an Angular project where I could use some assistance on how to filter data through checkboxes within a table. The setup involves a home component that displays data from a JSON server in a tabular format using a service ...

What is the method for defining the href attribute using the parameter passed through ejs?

For my node.js express application, I am utilizing ejs as the view engine. One of the pages in this application is a profile page that displays a user's information. Within this page, there is an anchor tag with the user's email address as the va ...

How can I prevent clicks on child links using the :not() selector?

I am working on a script using JQuery where I want to add a click handler to a div while ignoring clicks on the children a tags within it. You can check out my attempt (which is not working as expected) on this JSFiddle link: http://jsfiddle.net/q15s25Lx/ ...

SyntaxError: End of input caught unexpectedly (NodeJS TCP server)

My simple tcp server has the capability to send and receive json data. Here is a snippet of my code: // Handling incoming messages from clients. socket.on('data', function (data) { var obj = JSON.parse(data); if(obj.type == "regis ...

Attempt to import Recharts into React was unsuccessful

I am currently exploring the use of recharts in a React project, but I am facing difficulties when trying to import components from its library. I have added it to my package.json file as follows: "recharts": "^2.0.9", However, I am ...

Ways to conceal CSS on the page when triggering a different element

I am trying to achieve the functionality of hiding the black arrow when clicking on the green arrow, all without using jQuery. Here is my fiddle: http://jsfiddle.net/t5Nf8/195/ html: <div class="arrow-down"></div> <div class="arrow-up"> ...

What causes req.sessions to show an empty object instead of the expected value?

I've been grappling with a small issue while learning express.js. I am struggling to save sessions in the browser so that users don't have to log in every time they visit. I am using cookie-session for this purpose. When I send the login data fro ...

Structure of Divs with Bootstrap

Looking to create a layout with two divs side by side like the image below: Example However, when the screen width increases, the layout changes to this: Current Here is the code: Is there anyone skilled in this area who can guide me through it? Your a ...

Tips for making the first two columns of a datatable stand out with a unique color scheme

I'm attempting to customize the appearance of the datatables' CSS. Specifically, I want the first two columns to stand out by highlighting them in grey, while keeping the rest of the columns in their default color. Here is the code snippet I tri ...

Suggestions for merging 2 distinct :host() selectors with contrasting styles in Angular

I am facing a challenge with combining the CSS of two Angular components into one file. The issue arises from the difference in the :host() code for each component. For example: style1.css includes: :host() { flex: 2; overflow: auto; } style2.css ...

What is the equivalent of $.fn in AngularJS when using angular.element()?

Currently, I am conducting a directive unit test using jasmine. The test is now functional, but I need to find an alternative for $.fn in angularjs since the use of $ is prohibited in my workplace. Code: (function scrollTopEventDirective(application) ...

Tips for Positioning a Page Layout in the Center Using HTML and CSS

I'm a beginner in HTML and CSS and I'm trying to create a simple webpage layout from scratch. I want all the elements of the page (nav, header, section, footer, etc.) to be centered while still keeping the nav bar on the left side. I've set ...

Using jQuery to move a div to a specific location on the page

Is there a way to translate the position of div x and y using Jquery that is compatible with all browsers, such as IE 7, IE8, IE9, and IE10? I have attempted the following: <div id="s1" style="-ms-transform:translate(159,430)"> hello ...

When integrating matter-js and applying a Body using fromVertices, the alignment of the resulting vertices may be inaccurate in relation to one another

Take a look at the shape I'm anticipating. The vertices along the edge outline the points. https://i.sstatic.net/YpQrR.png This is the json file (derived from the image above) that I'm importing and using to construct the Body. { "cann ...

Creating a visually appealing multi-bar chart in AngularJS: Tips for effectively presenting data

Imagine that I have the JSON data below: [ { month: "Jan", cost: 80, energy: 90 }, { month: "Feb", cost: 50, energy: 80 }, { month: ...

What is the method for producing an li and anchor tag using a list object?

Here is the response I received from my web service, and now I need to transform it into a list item tag: {"d":[{"name":"ttt","url":"bbbb"},{"name":"uuu","url":"ppp"}]} How can I create li tags based on the above output? This is the desired format for t ...

Retrieving text content from multiple classes with a single click event

There are numerous elements each having class names p1, p2, p3,...p14. Consequently, when attempting to extract text from the clicked class, text from all classes is retrieved! For instance, if the expected text is 80, it ends up being 808080080808080808 ...

What is the best way to connect my products on an ecommerce site with hundreds of images?

Despite thoroughly searching the internet, I have been unable to find a solution to my dilemma. I am creating a platform that showcases a lengthy list of products on the website. Additionally, I have a database of pictures stored locally that need to be ...