【问题标题】:Multiple Parameter passing in setTimeOut function throws Uncaught ReferenceError: method is not defined (anonymous function)?在 setTimeOut 函数中传递多个参数会抛出 Uncaught ReferenceError: method is not defined (anonymous function)?
【发布时间】: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


【解决方案1】:

这些行不合逻辑:

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);
    }
})

您可能还想用!=== 替换===,在那里。我想这是为了检查参数是否已填写。

【讨论】:

  • 你是对的,为什么我使用 === 而不是 !=== ,但即使我在 JSONStore.get() 中使用 openChat,我也会收到“Uncaught ReferenceError: method is not defined "
  • 你问我的方法是在 JSONStore.get() 的回调中调用 openChat()。我剪切粘贴 if(regUserName===undefined || regUserMobile===undefined) { openChat(regUserName,regUserName); } 在 JSONStore.get() 回调中
【解决方案2】:

我尝试了您的代码,但遇到了与您提到的相同的错误,我已经修改了代码,现在使用此代码只需根据您的需要修改 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);
    }
}

我知道这是不同的方式,可能不建议使用,但我就是这样解决的。

【讨论】:

    【解决方案3】:

    我厌倦了这件作品及其正常工作:

    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()
    

    【讨论】:

    • Jyoti:简单的方法无疑有效,但我的情况是我使用 setTimeOut 的情况不同。
    • 我试过你的代码,也只是在函数 post() 中评论新行,一切正常。
    • 从 var collectionName = 'Registration' 开始; to alert("Error is "+err); });
    • 呵呵呵呵...这是主要技巧...您没有正确理解问题...您评论的代码是IBM worklight sn-p,用于提取数据,这很耗时并且由于异步性java脚本不等待提取并继续执行进一步的代码。所以我不得不使用 setTimeOut 声明“直到我的 JSONStore 没有被提取,请等待来自 JSONStore 的数据”..正因为如此,有人否决了你的答案......你的 Soln 是正确的,但它不适合这种情况
    • 我认为异步性质不应该对语法产生影响。