How to access event.clientX outside of window function? [duplicate] - javascript

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 months ago.
I tried to use the event.clientX outside of window function. But it says xAxis is not defined. Can have an way to get it? My code below,
window.onclick = function(event) {
let xAxis = event.clientX;
}
alert(xAxis);

Related

JavaScript and geolocation [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 1 year ago.
)
So I'm trying to learn about JavaScript and geolocation, but I've run into this, probably simple, problem:
let myLat; // Global
navigator.geolocation.getCurrentPosition((position) => {
myLat = position.coords.latitude;
console.log(myLat); // works
});
console.log(myLat); // undefined
I don't get why I cannot just use a global variable to hold the latitude-info?

jQuery - accessing variable outside onchange event? [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I'm trying to make my code dependent on certain variables by changing them with a select. Is it possible to access the result outside of the function in jQuery?
js
function myCalendar(){
//global variable
var thismonth = '';
//onchange get selected month
$(document).on('change','#select_month',function(){
thismonth = $(this).val();
});
//check if empty
if(thismonth !==''){//always return empty?
var mm = thismonth;
}else{
var mm = new Date().getMonth();
}
}

Get chrome storage [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 4 years ago.
I want to get chrome storage.
but outside of chrome.storage.local.get function It's not working.
My code is below
chrome.storage.local.get('Setting', function(data){
var Setting = data.Setting;
});
console.log(Setting);

Javascript json response undefined [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I have function like this:
function GetLala() {
var lala = $.getJSON(uriLala).done(function (data) {
return data;
});
return lala;
}
Then code like this:
var data = GetLala();
console.log(data.responseJSON);
This returned me undefined. If I type this code in google chrome console - then work.

Accessing a variable outside of a closure [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I'm pretty confused by the JavaScript scoping. I have a call to FB.api (shown below) that I would like to return a response object:
(function() {
var permissions;
FB.api("/me/permissions", "get", {access_token: options.token}, function(r) {
permissions = r;
});
console.log(permissions);
})();
Ideally, I'd like to assign FB.api to a variable, and use the anonymous function to return the r component, but it appears like that's not doable.
Why is permissions undefined? Is there a way to set permissions in the parent scope?

Categories

Resources