简体   繁体   English

全局变量未在函数内部更新

[英]Global variable not updated inside function

I spent few hours trying to find the reason why the variable dataset_path which is a global variable is always undefined 我花了几个小时试图找出为什么始终是未定义全局变量的dataset_path的原因

var dataset_path ;

function myFunction() {
  offline_data_mainlooper_bitfinex();
  print_bilan();
}
function offline_data_mainlooper_bitfinex(){
    var hours_step_size = 1*2;
    var daily_step_size = 24*2,
    // dataset_path = './quote/bitfinex/from_1480550400to_1512100800_1hquote.json';
    // dataset_path =  ('./quote/bitfinex/from_1480550400to_1512100800quote.json') //thats's 30 min
    dataset_path =  './quote/bitfinex/from_1480550400to_1512100800_15mquote.json'
       //... somecode


}
function print_bilan(){
   var filename = './analysis/test_log.json';
   //... somecode
 console.log('Quote saved! to ' +dataset_path, filename)
}

Because of this: 因为这:

var daily_step_size = 24*2,
//                        ^
dataset_path =  './quote/bitfinex/from_1480550400to_1512100800_15mquote.json'

That's declaring multiple variables in the local scope of offline_data_mainlooper_bitfinex , not ending the daily_step_size declaration and then doing an assignment to the global variable. 那是在offline_data_mainlooper_bitfinex的局部范围内声明多个变量,而不是结束daily_step_size声明,然后对全局变量进行赋值。 Replace the comma by a semicolon (or omit it). 用分号代替逗号(或忽略它)。

It worked for me when I put a ; 当我放一个; instead of a , after 而不是后

var daily_step_size = 24*2,

I think you are declaring a new var which scope is inside the function. 我认为您是在声明一个新的var范围在函数内部。 So when you're calling the global one, it's undefined. 因此,当您调用全局变量时,它是不确定的。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM