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);
});