Run mouseover function on Avascript only once

Asked

Viewed 236 times

0

I have a function that does some actions inside my html, the main is, this function does a request with ajax and returns me a list that is included directly in select. need q she make request only once when pass the mouse! because every time I move the mouse it makes a new request and updates select someone can help me?

$(".linha").mouseover(function () {

        var thDescricao = $(this).children('th.th-descricao');
        var thSimilar = $(this).children('.th-similar').children('select');

        var descricao = thDescricao.text();
        $.ajax({
            type: 'GET',
            url: 'similaridade/' + descricao + '/',
            success: function (data) {
                thSimilar.html(data);
            },
            error: function (data) {
                thSimilar.html(data);
            },
        });
});
  • Creates a variable with value 0, when passing the mouse feeds it with value 1 and checks whether the value is 0 or 1, if it is 1 does not execute AJAX

  • missing a complement is a table need to do this for each row of it

  • variable blocks when doing on other lines

1 answer

3


You can use the .one( jQuery. It will fire at most once per element and per event. In your case it would be:

$(".linha").one('mouseover', function () {
  • 1

    Worked perfectly, makes the request once for each line

  • 1

    Mutio thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.