Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一个函数产生两个不同的结果?回叫地狱_Javascript_Jquery_Callback - Fatal编程技术网

Javascript 一个函数产生两个不同的结果?回叫地狱

Javascript 一个函数产生两个不同的结果?回叫地狱,javascript,jquery,callback,Javascript,Jquery,Callback,在HTML中: TABS.cutoffs = {}; TABS.cutoffs.initialize = function(callback) { var self = this; $("select[name=selectlanguage]").on("change", function() { localStorage.setItem("languageValue", $(this).val()); console.log(localStorage.getItem(

在HTML中:

TABS.cutoffs = {};
TABS.cutoffs.initialize = function(callback) {
  var self = this;

  $("select[name=selectlanguage]").on("change", function() {
    localStorage.setItem("languageValue", $(this).val());
    console.log(localStorage.getItem('languageValue'), "inside$JqueryFunction");
  });

  console.log(localStorage.getItem('languageValue'), "outsidetheFunction");

  if (callback) callback();

};
看到了吗?它应该是相同的,就像:

en outsidetheFunction
zh inside$JqueryFunction


为什么
localStorage.getItem('languageValue')
在函数
$(“select[name=selectlanguage]”)内外变得不同。在(“change”,function()

中没有回调地狱

以下是调用
initialize
函数时发生的情况:

  • 更改事件处理程序已设置(但未调用)
  • 本地存储在控制台中读取和打印
另一个时间——我猜,以后——如果您更改select中的select选项,新的select值将写入本地存储

因此,你得到的情况非常正常:

首先,
“en outsidetheFunction”
是在控制台中打印的。很可能是因为“en”是在本地存储中编写的。我想是以前执行的

然后,
“zh inside$JqueryFunction”
将在控制台中打印。 很可能是因为您在select中选择了“zh”,它触发了更改事件,该事件在localStorage中写入并打印了新值。

因为在事件处理程序外部记录后它会更改?
var a=1;console.log(a);a=2;//为什么控制台会说a是1?
en outsidetheFunction
zh inside$JqueryFunction
en outsidetheFunction
en inside$JqueryFunction
zh outsidetheFunction
zh inside$JqueryFunction