This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
This gives me the content of a file, in an alert box. As long as the alert is inside the get-functions block it's working fine:
var allActivities = [];
$(document).ready(function() {
$.get('./activities.txt', function(result) {
currentActivity = result.split(",");
allActivities.push(currentActivity);
alert(allActivities); // Alert box returns correct results
});
}
But when I move the alert outside of the get-function scope, the alert box is empty. I'm guessing I have messed up with some scopes, but I can't figure out what.
var allActivities = [];
$(document).ready(function() {
$.get('./activities.txt', function(result) {
currentActivity = result.split(",");
allActivities.push(currentActivity);
});
alert(allActivities); // Alert box is empty
}
I need the array allActivities to be global so that I can access it outside of the .get function. So I need the last code example to work. In other words, the last code example needs to alert the content.
Gathered from comments:
Since $.get is asynchronous the alert is called before my array is populated. The global array is working as expected. The alert needs to be called at a later point, for instance with a button click after page load.
I have tested it and it worked.
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
This line of code is getting document count from firestore database it works fine when i call the variable inside the function but outside its undefine.
var LM35TOTALRES; // I WANT TO STORE HERE
db.collection("LM35").get().then(function(querySnapshot) {
LM35TOTALRES = querySnapshot.size //DATA THAT I WANT
console.log(LM35TOTALRES); // THIS IS WORKING
});
console.log(LM35TOTALRES); // NOT WORKING
I believe this is because the function is async so it finishes setting the value after the log outside, One solution is to return the value needed in the variable directly and await it like so const LM35TOTALRES = await (db.collection("LM35").get()).size
This occurs due event loop scheduling you can find some good examples in this video about min 2 or so
use let instead of var
let LM35TOTALRES;
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
(14 answers)
Closed 3 years ago.
Hello gurus in the house. It sounds like a duplicate but I am having a tough time with it since 48hrs past. The question is based on a jQuery. I have a file that i would like to pass into a variable then later use the variable in other places of my code. The file only contains 'I love you'.
<script>
var kk;
$.get('k.txt', function(data) {
kk = data;
}, 'text');
alert(kk); //undefined
</script>
The result of the alert is undefined and it does not work in opera browser. I can alert it like this.
<script>
var kk;
$.get('k.txt', function(data) {
alert(kk); //alert is fine
}, 'text');
</script>
I don't mind if you can show me any other working process apart from this. Please I need this so badly and I have being dealing with this since yesterday. Thank you sirs for understanding.
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
Don't know if I'm being blind but every time I look at this code the logic makes sense. I am trying to iterate through this for loop and for each iteration generate a dialog box with the ID of #dialog-(i) but it only show's the last iteration of 200. The code is below:
var i;
for(i=1;i<200;i++){
$("#dialog-" + i).hide();
$('#meetings_box-' + i).click(function() {
var dialog = $("#dialog-" + i).dialog();
if (dialog) {
console.log('yay');
console.log(dialog);
} else {
console.log('nay');
}
});
};
Any help to finding the issue, probably something really dumb
This is because click will happen sometime in the future and by then the loop has already finished it's execution and the value of i is updated to the last value. Instead of var you can use let
This question already has answers here:
How to pass parameter to function using in addEventListener?
(4 answers)
Closed 6 years ago.
I got a problem when trying to make onchange listener for input in JavaScript:
var apple = document.getElementById("apple");
apple.addEventListener("change", validate("apple"));
function validate(fruitName){
var qty = parseInt(document.getElementById(fruitName).value);
console.log(qty);
}
I am trying to check everytime when the user input something for 'apple' input, I will print the quantity at console. But by doing this, It only ran the first time when I refreshed the browser by printing out a NaN and does not work even when I changed the input for 'apple'.
Any ideas?
Thanks in advance.
You have to reference the function, not call it, and you don't need to pass in the name to use as an ID to get the elements, the element is already available in this
var apple = document.getElementById("apple");
apple.addEventListener("change", validate);
function validate(event){
var qty = parseInt(this.value, 10);
console.log(qty);
}
Whenever you add parentheses you call the function, and whatever is returned is passed to the event handler.
As functions return undefined by default, that's what you get.
This question already has an answer here:
about chrome.tabs.executeScript( id,details, callback)
(1 answer)
Closed 8 years ago.
I am injecting a script into a tab using chrome.tabs.executeScript. Its last parameters is a callback function that will be called with The result of the script in every injected frame. . How do I make my script return a result? I tried adding a simple return statement at the end of my js, but nothing got returned.
As RobW corrected me, a function is not necessary, just make sure that the desired data is the last expression and of course that is JSON-serializable.
chrome.tabs.executeScript(tabId,
{code:"document.charset"},
function(results){
// results[0] will now contain the charset for the page in question
});