
Untitled
By: a guest on
Aug 7th, 2012 | syntax:
None | size: 1.08 KB | hits: 12 | expires: Never
How to solve this JavaScript scope issue
var news;
$.get('news.txt', function (dat) {
news = dat;
if ($('#removeN').is(':checked')) {
news = "";
}
alert(news) // Displaying exact result;
});
alert(news) // Displaying undefined..; Why?
var news;
$.get('news.txt', function (dat) {
news = dat;
if ($('#removeN').is(':checked')) {
news = "";
}
alert(news) // BEFORE THIS!
});
alert(news) // THIS EXECUTES
$.get('news.txt', function (dat) {
news = dat;
if ($('#removeN').is(':checked')) {
news = "";
}
doSomething(news) // Displaying exact result;
});
var doSomething = function(data) {
alert(data);
}
var news;
$.get('news.txt', function (dat) {
//process news
}).done(function(dat){
//display news, or maybe more processing related to this particular function block
alert(news);
}).fail(function(){
//oops, something happened in attempting to get the news.
alert("failed to get the news");
}).always(function(){
//this eventually gets called regardless of outcome
});