0

When a searchbox is submitted, I execute 2 API functions.

document.getElementById('getWeather').onclick = function(){
  // init data stream
  getAPIdata();
  getAPIWeerdata();
};

The first one getAPIdata(); is for the searchbox itself and of course the value can change when the user submits again. So this can get executed more than once.

The second one on the other hand getAPIWeerdata(); gets executed once when the user submits the searchbox.

Can I create something like getAPIWeerdata();.ExecuteOnce

epascarello
  • 204,599
  • 20
  • 195
  • 236
Blank
  • 540
  • 2
  • 21

2 Answers2

2

One solution would be to use a boolean that is set to false when you call that onclick function, and call getAPIWeerdata() only if that bool is true. Make sure to call getAPIWeerdata() before you set your bool to false.

So :

var boolean = true;

document.getElementById('getWeather').onclick = function(){
// init data stream
getAPIdata(); 
if (boolean) {
getAPIWeerdata();
}
boolean = false;
};
stacks
  • 349
  • 2
  • 5
  • 13
0

Set a data- attribute on the respective dom element:

document.getElementById('getWeather').onclick = function(){
    // init data stream
    getAPIdata();
    if (!document.getElementById('getWeather').hasAttribute('data-inited')) {
        getAPIWeerdata();
        document.getElementById('getWeather').setAttribute('data-inited', 1'); // Value does not matter
    }
}
collapsar
  • 17,010
  • 4
  • 35
  • 61