【发布时间】:2025-12-18 04:55:02
【问题描述】:
我有一个函数A() 调用函数B(x, y); 再次调用C(p,q);
A() --> B(x,y) -->C (p,q)
function A() {
B(x,y)
}
B() 处理 JSONStore 以提取与函数 C 相关的数据,在 JSONStore 数据提取结束之前,脚本调用 func C(p,q) 为了避免它,我使用了延迟 1 秒的 setTimeOut。
function B(x,y) {
if(p===undefined || q===undefined) {
setTimeout(function() {
C(p,q);
}, 1000);
}
}
但我收到错误,因为 Uncaught ReferenceError: method is not defined M979:192 C VM979:192 (anonymous function)
function C(p,q) {
.........
}
我阅读了许多与 setTimeOut 相关的博客,并发现了这种方法。
我的代码:
function Submit_Data() {
var ChatWindow_Height = 650;
var ChatWindow_Width = 570;
window.open("Live Chat", "chat", "height=" + ChatWindow_Height + ", width = " + ChatWindow_Width);
post("https://xyz/abc/StartChat.aspx", "post");
}
var regUserName,regUserMobile;
function post(path, method) {
method = method || "post"; // Set method to post by default if not specified.
var collectionName = 'Registration';
var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = {uName: 'string'};
WL.JSONStore.init(JSONStoreCollections)
.then(function () {
WL.JSONStore.get(collectionName).findAll().then(function (res) {
WL.Logger.info('Registration retrived is :', res);
console.log(JSON.stringify(res));
console.log(res[0].json.userName+" "+res[0].json.userMobile+" "+res[0].json.userPass+" "+res[0].json.userRePass);
regUserName=res[0].json.userName;
regUserMobile=res[0].json.userPass;
console.log("For Chat Data 1 is "+regUserName+" "+regUserMobile);
})
.fail(function (err) {
WL.Logger.error("Failed authentication "+err);
});
})
.fail(function (err) {
alert("Error is "+err);
});
if(regUserName===undefined || regUserMobile===undefined) {
setTimeout(function() {
openChat(regUserName,regUserName); // Error Here
}, 1000);
}
}
function openChat(regUserName,regUserName) {
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
form.setAttribute("target", "chat");
var hiddenField1 = document.createElement("input");
var hiddenField2 = document.createElement("input");
var hiddenField3 = document.createElement("input");
console.log("For Chat Data 3 is "+regUserName+" "+regUserMobile);
hiddenField1.setAttribute("type", "hidden");
hiddenField1.setAttribute("id", "vName");
hiddenField1.setAttribute("name", "vName");
hiddenField1.setAttribute("value", regUserName);
hiddenField2.setAttribute("type", "hidden");
hiddenField2.setAttribute("id", "mobile");
hiddenField2.setAttribute("name", "21512");
hiddenField2.setAttribute("value", regUserMobile);
hiddenField3.setAttribute("type", "hidden");
hiddenField3.setAttribute("id", "state");
hiddenField3.setAttribute("name", "21524");
hiddenField3.setAttribute("value", "25");
document.body.appendChild(form);
form.submit();
}
【问题讨论】:
-
你为什么用setTimeout而不是回调函数?
-
嗯,看起来
openChat应该在范围内。但是,我不明白您为什么使用setTimeout而不是将调用放在JSONstore 回调中? -
是的,我应该将 openChat 放在 JSONStore 回调中。实际上这是我做的第一种方法,我得到了同样的错误 Uncaught ReferenceError: method is not defined...
-
要么您声明它与您向我们展示的不同,要么您将
undefined分配给openChat某处。这是你所有的代码吗? -
Bergi:作为一种解决方法,我注释掉了 openChat 函数并将其逻辑放在 JSON.get 回调 Success sn-p 中。如果您看到我在 OpenChat 方法中以 POST 形式提交表单,那么现在我已将该表单提交代码放在我的 setTimeOut 部分中。这样,我可以等待 1 秒将表单提交到服务器,这意味着在这 1 秒内我可以从 JSONStore.get 回调中获取数据。
标签: javascript ibm-mobilefirst settimeout