我有一个函数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)
以避免它我使用setTimeOut并延迟1秒
function B(x,y) {
if(p===undefined || q===undefined) {
setTimeout(function() {
C(p,q);
}, 1000);
}
}
但是我收到错误,因为Uncaught ReferenceError:方法未定义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();
}
答案 0 :(得分:1)
这些行不合逻辑:
if(regUserName===undefined || regUserMobile===undefined) {
setTimeout(function() {
openChat(regUserName,regUserName); // Error Here
}, 1000);
}
只需在openChat
回调中致电JSONStore.get()
:
regUserMobile=res[0].json.userPass;
console.log("For Chat Data 1 is "+regUserName+" "+regUserMobile);
if(regUserName===undefined || regUserMobile===undefined) {
openChat(regUserName,regUserName);
}
})
您也可能希望将===
替换为!===
。我想这可能是为了检查参数是否已填满。
答案 1 :(得分:0)
我尝试了你的代码并且我得到了与你提到的相同的错误,我已经调整了代码,现在使用这段代码只需根据你的需要修改URL链接。
function Submit_Data()
{
var ChatWindow_Height = 650;
var ChatWindow_Width = 570;
window.open("VodaFone 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.
// 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");
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.userMobile;
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");
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() {
form.appendChild(hiddenField1);
form.appendChild(hiddenField2);
form.appendChild(hiddenField3);
document.body.appendChild(form);
form.submit();
}, 1000);
}
}
我知道这是不同的方式,可能不是可取的,但这就是我如何解决它。
答案 2 :(得分:-2)
我厌倦了这件作品并且工作正常:
function A() {
B(1, 2)
}
function B(x,y) {
if(true) {
setTimeout(function() {
C(3,4);
}, 1000);
}
}
function C(p,q) {
alert('in c')
}
A()