ani_css
ani_css

Reputation: 2126

Get json data only one time

if I mouseover on my element 5 times then json data has been loading 5 times but I want when ever I mouseover my element only load json data for one times to.

$(document).on('mouseover', '.details-checkin,.details-checkout', function() {
  $.getJSON('http://content.anitur.com/web/Assets/js/data.json', function(data) {
    //bla bla blaaa
  });
});

Upvotes: 1

Views: 1142

Answers (2)

Miguel Baena
Miguel Baena

Reputation: 36

Did you consider using the sessionStorage? If you need to get JSON only once every session, you could add something like this:

sessionStorage.jsonWasRead = 'true';

if (sessionStorage.jsonWasRead != 'true') {
    //your read json code
}

Upvotes: 1

julian soro
julian soro

Reputation: 5228

You can add a simple check to see if you already made the call. This uses a variable scoped outside the event handler

var isLoading = false;
$(document).on('mouseover', '.details-checkin,.details-checkout', function() {
  if(isLoading === false) {
    isLoading = true
    $.getJSON('http://content.anitur.com/web/Assets/js/data.json', function(data) {
      //bla bla blaaa
    });
  }
});

Upvotes: 2

Related Questions