Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 为什么setTimeout无法访问外部特定变量?_Javascript_Settimeout_Client Side - Fatal编程技术网

Javascript 为什么setTimeout无法访问外部特定变量?

Javascript 为什么setTimeout无法访问外部特定变量?,javascript,settimeout,client-side,Javascript,Settimeout,Client Side,我发现setTimeout有一种奇怪的行为。我不知道为什么它能够从setTimeout之前声明的变量值中正确选择一些变量值,但不能选择特定的变量值 this.RegisterForUpdateContents = function (intervalTime, FormControlGuid) { var layoutInfo = sessionStorage.getItem('LayoutInfo'); var panelName =

我发现
setTimeout
有一种奇怪的行为。我不知道为什么它能够从
setTimeout
之前声明的变量值中正确选择一些变量值,但不能选择特定的变量值

this.RegisterForUpdateContents = function (intervalTime, FormControlGuid)
{        
    var layoutInfo = sessionStorage.getItem('LayoutInfo');            

    var panelName = "Panel_" + FormControlGuid;

    var timeOut = parseInt(intervalTime) * 1000;
    var self = this;

    var timeoutHandle = setTimeout(function ()
    {            
        var dashboardViewer = "dashboardViewer_" + FormControlGuid;
        console.log('Just sending callback for Update and LayoutInfo is  : ' + layoutInfo);

        var CallbackInfo = { 'selectedClientId': ClientId_HeaderSection.GetValue(), 'PanelName': panelName, 'Width': dockPanel.width, 'Height': dockPanel.height, 'dashboardViewer': dashboardViewer, 'UpdateTime': intervalTime, 'Layout': layoutInfo };  

    }, timeOut);


    sessionStorage.setItem((panelName + "|" + "Timeout"), timeoutHandle);
}
我可以访问
panelName
内容,但我不知道
layoutInfo
发生了什么
layoutInfo
始终为空作为
“”
。但是如果我从
sessionStorage.getItem('LayoutInfo')
访问
setTimeout
内部,它是可以访问的

有人知道为什么吗

编辑

$.each(keys, function (index, singlePanel)
{
  sessionStorage.setItem(singlePanel, JSON.stringify(panelRenderingInfo));
  if (UpdateTime)
      selfInstance.RegisterForUpdateContents(UpdateTime, FormControlGuid);
});

var LayoutVSClient = { 'CurrentLayout':    selectedLayout,'selectedClientValue': ClientId_HeaderSection.GetValue() };
sessionStorage.setItem('LayoutInfo', JSON.stringify(LayoutVSClient));

我刚刚重新创建了您的代码,使其尽可能接近一个新的版本

使用此示例,我无法复制您描述的问题。您是否有一行代码用于设置
会话存储
?例如:

sessionStorage.setItem('LayoutInfo', 'layoutInfoContent');
==编辑===

@Uston,您是否可以将上面新编辑的代码更改为以下内容:

var LayoutVSClient = { 'CurrentLayout':    selectedLayout,'selectedClientValue': ClientId_HeaderSection.GetValue() };
sessionStorage.setItem('LayoutInfo', JSON.stringify(LayoutVSClient));

$.each(keys, function (index, singlePanel)
{
  sessionStorage.setItem(singlePanel, JSON.stringify(panelRenderingInfo));
  if (UpdateTime)
      selfInstance.RegisterForUpdateContents(UpdateTime, FormControlGuid);
});

我认错了

在调用RegisterForUpdateContents之前,我必须使用LayoutInfo键设置sessionStorage。
@Ant:谢谢你的观点。

第一个也是最简单的问题是:你确定sessionStorage在试图检索该键时保存了你期望的值吗?因为您正在访问该变量。若它给你们一个空字符串,那个么你们确实得到了一个值,这不像它给了未定义的值或者说变量不存在。我相信,如果会话存储中不存在键,它将返回null,而不是空字符串。此外,要了解sessionStorage是否是该变量未显示超时预期内容的部分原因,请将该行更改为“var layoutInfo=‘test string’;”?不,绝对不是,此会话存储100%具有相应正确值“LayoutInfo”的密钥。这就是为什么sessionStorage.getItem('LayoutInfo')可以很好地访问setTimeOut中的内容。我认识到我的错误。我认为这是因为在注册setTimeout中要调用的代码之前没有设置sessionStorage。它正在发生@蚂蚁:看编辑。啊,我明白了。刚刚对我的答案也做了修改。希望在这里改变事情的顺序能有所帮助。不过,我不确定它是否会破坏您案例中的任何现有功能。Ant:是的,编辑成功,一切正常…:-)感谢您的帮助并指向代码中sessionStorage的设置。如果提供的解决方案对您有所帮助,请不要忘了将其标记为已接受的答案。:)我现在不能把它当作答案。只是等待另一天…:-)哦,我指的是提供的答案,包括JSFIDLE和代码示例。
var LayoutVSClient = { 'CurrentLayout':    selectedLayout,'selectedClientValue': ClientId_HeaderSection.GetValue() };
sessionStorage.setItem('LayoutInfo', JSON.stringify(LayoutVSClient));

$.each(keys, function (index, singlePanel)
{
  sessionStorage.setItem(singlePanel, JSON.stringify(panelRenderingInfo));
  if (UpdateTime)
      selfInstance.RegisterForUpdateContents(UpdateTime, FormControlGuid);
});