在setTimeOut函数中传递多个参数会抛出Uncaught ReferenceError:方法未定义(匿名函数)
Multiple Parameter passing in setTimeOut function throws Uncaught ReferenceError: method is not defined (anonymous function)?
我有一个函数A()
调用函数B(x, y);
再调用C(p,q);
A() --> B(x,y) -->C (p,q)
function A() {
B(x,y)
}
B()
处理JSONStore提取与函数C
相关的数据,在JSONStore数据提取结束之前,脚本调用函数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();
}
这些行是不合逻辑的:
if(regUserName===undefined || regUserMobile===undefined) {
setTimeout(function() {
openChat(regUserName,regUserName); // Error Here
}, 1000);
}
在JSONStore.get()
回调中调用openChat
:
regUserMobile=res[0].json.userPass;
console.log("For Chat Data 1 is "+regUserName+" "+regUserMobile);
if(regUserName===undefined || regUserMobile===undefined) {
openChat(regUserName,regUserName);
}
})
您可能还想将===
替换为!===
,在那里。我想这是为了检查参数是否被填满。
我尝试了你的代码,我得到了同样的错误,因为你已经提到,我已经推了代码,现在它的工作与此代码只是修改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);
}
}
我知道这是不同的方式,可能不可取,但这就是我如何修复它。
我累了,它的工作正常:
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()
相关文章:
- 如何确保函数/方法参数为certian类型
- Javascript中对象中的函数方法
- Object.assign(),函数方法和内存使用
- 我应该在原型上还是在新创建的实例上调用构造函数方法
- Angular promise回调不是在构造函数方法内部触发,而是在对象文本方法中触发
- 如何使用 Karma、Jasmine 和 Istanbul 指定测试应涵盖哪些函数/方法
- 在ES2015中定义一个类,构造函数方法是什么以及为什么它是必不可少的
- 如何调用通过它传递不同 ID 的函数方法
- Sencha ExtJs 为什么从“构造函数”方法返回“this”
- 这个 JavaScript 函数方法调用了什么
- 如何在 javascript 中从子原型调用父类的构造函数方法
- 是否仍可访问被覆盖的函数/方法
- 在构造函数之外更改 JavaScript 中的构造函数方法
- 这种实例化构造函数方法的风格是什么
- 如何在 JavaScript 中使用继承与构造函数方法一起使用 返回具有私有属性的对象文本
- 我的javascript构造函数方法以文本形式返回整个方法,而不是预期的返回值
- 如何在事件中将变量传递给构造函数方法
- 如何限制用户在ui ace编辑器中只能编写一个javascript函数(方法)
- 打印任何java应用程序的函数/方法调用序列
- 如何在jQuery语句中使用Javascript构造函数方法