Tips for executing a function just one time within a mouseover function in TypeScript

I have a project in Angular 7 where I am displaying a list of places. Whenever I hover over a place, an AJAX function is triggered to save that information in the database.

However, I noticed that the AJAX function is being called multiple times when I hover over a single element. I only want it to be called once.

Below is the Angular code snippet I am using:

<div *ngFor="let place of mapData" (mouseover)="displayTagInfo($event)">
  <div class="filter-name">{{place.name}}</div>
</div>

Answer №1

Within the mapData array object, add a key called hovered = false with an initial value of false. For every element where hovered = false, trigger the function displayTagInfo($event,place).

<div *ngFor="let place of mapData" (mouseover)="displayTagInfo($event,place)">
  <div class="filter-name">{{place.name}}</div>
</div>

In your TypeScript file:

displayTagInfo(event,place){
  if(!place.hovered){
    //Do whatever you need to do in this block

    place.hovered = true;
  }
}

This method ensures that each element can only be hovered over once. Best of luck!

Answer №2

Essentially, the solution is to incorporate a boolean variable within the function that monitors whether the event has been triggered or not.

To properly implement this functionality, it is essential to initialize the variable within the constructor of the specific component you wish to monitor.

function displayTagInfo(event){
    if (this.tagInfoDisplayHasBeenFired) {
        return;
    }

    this.tagInfoDisplayHasBeenFired = true

   //Place your code here
}

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

Having trouble setting up tailwind css for my Angular project, need some help

Initially, I was referencing this article (along with others): https://medium.com/@jacobneterer/angular-and-tailwindcss-2388fb6e0bab I've been attempting to integrate Tailwind into my Angular project without success. Additionally, I even started an A ...

Interact with various contenteditable sections by navigating with the arrow keys

I have a challenge with multiple <p contenteditable="true"></p> elements on my page. I am seeking a solution to enable the use of arrow keys to navigate seamlessly across these separate elements as if they were one cohesive editable element. F ...

Comparing the previously selected node with the currently selected node in an ASP.NET treeview

I am looking to implement a comparison between the last selected node and the currently selected node on a tree view using JavaScript. Can anyone provide me with some code snippets to help me compare the previous selection with the current selection on th ...

AmplifyJS is throwing an error: TypeError - It seems like the property 'state' is undefined and cannot be read

I am currently working on integrating the steps outlined in the Amplify walkthrough with an Angular cli application. My app is a brand new Angular cli project following the mentioned guide. My objective is to utilize the standalone auth components a ...

Displaying elapsed time in JavaScript (Node.js)

Looking for a way to convert a date time or time-stamp format like 2015-12-18 07:10:54 into a relative time, such as "2 hours ago." I attempted the code provided, but it seems to consistently show an incorrect estimation of "8 days ago." function convert ...

Calling a JavaScript function from server-side code (without using startup scripts)

In essence, my objective is as follows: Initiate deletion of a record upon button click. Delete the record only if a specific column (Location) is null (working perfectly). If the specific column is not null, prompt the user for confirmation before proce ...

Ways to display the SearchFilter textbox in an Angular Multiselect Dropdown, even when there are no items loaded

Angular version: 8 ng-multiselect-dropdown version: ^0.2.10 I am facing a situation where the user needs to start typing in the search field to load results dynamically into the dropdown. However, in the ng-multiselect-dropdown component, the search box ...

Real-time data update with Socket io: instantly refresh data on another page upon form submission without having to manually

What is the best way to utilize socket io in a situation where I have two pages open, one displaying a table of existing data and the other featuring a form for users to input new data? I want the table page to automatically update whenever a user submits ...

A guide on the correct way to cast ObjectIds in Mongoose

I've encountered a problem with casting ObjectId in express.js using mongoose. In my route, I've attempted both casting before and using req.params.id directly, but nothing seems to be working. I'm certain the id is correct as I've tri ...

Solving the Dilemma of Ordering Table Rows by Value in JavaScript

I am currently working on a table and my goal is to display rows in an orderly manner based on the sum of their columns. Essentially, I want the rows with the highest values to be displayed first, followed by rows with second highest values, and so on. Des ...

Show information in a table based on a unique identifier

I am working with some data that looks like this [ { date: '20 Apr', maths: [70, 80.5, 100], science: [25, 20.1, 30] }, { date: '21 Apr', maths: [64, 76, 80], science: [21, 25, 27] }, ]; My goal is to present ...

Guide on transmitting data from a child component to a parent object in Vue.js?

When I attempt to utilize a child component with custom form inputs and emit those values to the parent component, I encounter an issue where one input value disappears when another input value is entered. Let me showcase some code: Child Component <tem ...

What is the best way to send a confidential string from my Node.js backend to my Angular frontend without it being visible to the user?

Currently, I'm working on a project that involves Front-end development with Angular and Back-end with NodeJS. In the backend, I compile Angular static files using the command: response.sendFile(path.join(__dirname, 'dirname', 'index.h ...

Using Node.js: Only bring in the necessary function, don't execute the entire file

Today, I stumbled upon an interesting observation and I'm curious about the peculiar behavior of node in this scenario. I have two files structured as follows: src/api/index-api.ts src/worker/index-worker.ts Both of these files contain a simple con ...

Choose all the alternative A radio buttons utilizing JavaScript

If the "4 stars" option is selected at the top, I want all corresponding "4 stars" options in the form to be automatically selected. Similarly, if "3 stars" is chosen, I need all the "3 stars" options to be selected. I attempted to achieve this functionali ...

Converting JavaScript to PHP and encountering problems with POST encoding

Trying to send a string from JavaScript on the client-side to a PHP script on the back-end. The string has special quotes like ’ and “. When checking the console in Chrome, the POST headers show these special characters as they are. On the PHP side, I ...

An issue arose while attempting to pin a file on IPFS: A TypeError [ERR_INVALID_ARG_TYPE] was encountered, specifying that the argument "url" must be a string data type

Adding the files to another project resulted in an error that I am struggling to understand. When running "node scripts1/runScript.js", the error message received is as follows: Error occurred while pinning file to IPFS: TypeError [ERR_INVALID_ARG_TYPE]: ...

Facing issue with local redis session not functioning as intended

I'm encountering an issue with my redis session not functioning properly when testing locally. EDIT: Additionally, I realized that it's failing to save a cookie when trying to set req.session[somekey] as undefined like so: req.session.user = u ...

Editing JSON files - Substitute the $scope object with a new value

I am facing an issue with extracting data from an external JSON file for my application. Although most of the data loads into a DataTables table successfully, I am encountering problems with the $scope variable since it is referencing external data. Specif ...

MUI Gradient Tracked Button

Take a look at this Codepen example I created. It showcases a hover effect where the gradient follows the mouse cursor. In order to achieve this effect, I have defined two CSS variables - --x and --y, to keep track of the mouse position on the button. The ...