Reputation: 540
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
Upvotes: 0
Views: 86
Reputation: 17238
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
}
}
Upvotes: 0
Reputation: 359
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;
};
Upvotes: 2