As for the handler to modify the variable in one scope and use the result in another scope?

There is code:
window.onclick = async (event) => {
 let forecast;
 let location;
 if (event.target === document.querySelector('.navigation_menu_block-image_btn')) {
 if (document.querySelector('.search_menu_block-input').value > 2) {
 setImg(forecast, location);
 } else {
setImg();
}
}
 if (event.target === document.querySelector('.search_menu_block-submit')) {
 const areaSearch = document.querySelector('.search_menu_block-input').value;
 const currForecast = await getForecast(useGeocod(areaSearch));
 forecast = await currForecast;
 location = await useGeocod(areaSearch);
}
};


I need to call a function with arguments forecast and location in case if (document.querySelector('.search_menu_block-input').value > 2)
Adding value is another scope:
if (event.target === document.querySelector('.search_menu_block-submit')) {

 forecast = await currForecast;
 location = await useGeocod(areaSearch);
 }


How do I make the values set in scope if (event.target === document.querySelector('.search_menu_block-submit')) was taken into account when calling this function? :
if (document.querySelector('.search_menu_block-input').value > 2) {
 setImg(forecast, location);
 }
April 3rd 20 at 17:38
1 answer
April 3rd 20 at 17:40
Solution
What?
All done in order. Probably you need to swap the upper if blocks?
@orlo_Brekke If this block:
if (event.target === document.querySelector('.navigation_menu_block-image_btn')) {
 if (document.querySelector('.search_menu_block-input').value > 2) {
 setImg(forecast, location);
 } else {
setImg();
}
 }

call setImg(forecast, location) that the arguments will be undefined
I need to after the values are recorded here:
forecast = await currForecast;
location = await useGeocod(areaSearch);

it would be possible to use the value in the function call

And now always forecast and location undefined - stephan_Lemke commented on April 3rd 20 at 17:43
And seems to be reached.
It is obvious that the scope on the scope, is that it is local. In any case you need to use a higher scope that will pasarin for all calls.
Put the onclick before, outside of functions:
let forecast;
let location;
indow.onclick = async (event) => {
 // ...
}

Alternatively, use a closure:
indow.onclick = ((forecast, location) => async () => {
 // ...
})();

You can still petrocity by assigning forecast and location properties of the function:
indow.onclick = async function someFunction(event) {
 // ...
 setImg(someFunction.forecast, someFunction.location);
 // ...
 someFunction.forecast = ...
 someFunction.location = ...
 // ...
}
- orlo_Brekke commented on April 3rd 20 at 17:46
@orlo_Brekke, thank you very much
blunted
just to throw in scoop and all gut
thanks again - stephan_Lemke commented on April 3rd 20 at 17:49

Find more questions by tags JavaScript