开发者

Getting an array from $.post()

开发者 https://www.devze.com 2023-02-14 19:04 出处:网络
I am using: $.post(\'fromDB.php\', function(data) { eval(data); console.log(data); updateTimer(); }); to get some arrays from php.

I am using:

$.post('fromDB.php', function(data) {
    eval(data);
    console.log(data);
    updateTimer();
});

to get some arrays from php. What php returns:

var todayTimeMinutes = [0, 45, 35, 25, 40, 0, 50, 40, 40, 30, 20];
var todayTimeHours = [0, 8, 9, 10, 10, 11, 11, 12, 13, 14, 15];
var todaySect开发者_开发百科ionName = ["Before School", "Period 1", "Period 2", "Formtime", "Interval", "Period 3", "Period 4", "Lunchtime", "Period 5", "Period 6", "After School"];
console.log("Excecution time: 0.00058889389038086 seconds");

The console.log works fine. When I try to access values from the array inside the success function, it works fine. However, accessing it from updateTimer() does not work, and gives me this message in the chrome debugger:

Getting an array from $.post()


I'm guessing that you are attempting to access todaySectionName within updateTimer(). In that case, the reason you're getting the error is that todaySectionName is not in scope in updateTimer.

So you either need to define updateTimer as a closure within your success function, or you need to find a different way to pass those values to updateTimer. (Like as arguments.)

So wherever updateTimer is defined, change its signature to this:

function updateTimer(todayTimeMinutes, todayTimeHours, todaySectionName) {
    // leave this the same
}

Then change your success function to this:

$.post('fromDB.php', function(data) {
    eval(data);
    console.log(data);
    updateTimer(todayTimeMinutes, todayTimeHours, todaySectionName);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消