在setTimeOut函数中传递多个参数会抛出Uncaught ReferenceError:方法未定义(匿名函数)

Multiple Parameter passing in setTimeOut function throws Uncaught ReferenceError: method is not defined (anonymous function)?

本文关键字:函数 方法 ReferenceError 未定义 Uncaught setTimeOut 参数      更新时间:2023-09-26

我有一个函数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()