[英]Excecute script after defined value in variable without setTimeout in javascript
I wanted to execute a script after define value in variable without setTimeout. 我想在没有setTimeout的变量中定义值后执行脚本。
This is I am trying to do: 这是我正在尝试做的事情:
var sessionId;
function AddSessionID(e) {
sessionId = e;
}
$(function(){
alert(sessionId);
});
But I am getting value is undefined. 但是我得到的价值是不确定的。
If I set a delay for this like: 如果我为此设置延迟时间:
$(function(){
setTimeout(function(){
alert(sessionId);
}, 2000);
});
then I am getting proper value. 那么我就得到了适当的价值。
But I don't want to set delay for that. 但是我不想为此延迟。 because session id is dynamic.
因为会话ID是动态的。 it is depended to the server.
它取决于服务器。
I need a call back. 我需要回电。
This is really I wanted: 这真的是我想要的:
var sessionId;
function AddSessionID(e) {
sessionId = e;
}
$(function(){
if (sessionId != undefined) {
alert(sessionId);
}
});
write a callback like this - 编写这样的回调-
function AddSessionID(e) {
sessionId = e;
sessionIdReadyCallback();
}
function sessionIdReadyCallback(){
alert(sessionId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.