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

StackOverflow https://stackoverflow.com//questions/24009001

문제

I have a function A() which calls function B(x, y); which again calls C(p,q);

A() --> B(x,y) -->C (p,q)

function A() {
    B(x,y)
}

B() Processes JSONStore to extract data relevant for function C, Before JSONStore data extraction ends script makes a call for func C(p,q) so to avoid it I used setTimeOut with a 1 second delay.

function B(x,y) {
    if(p===undefined || q===undefined) {
        setTimeout(function() {
            C(p,q); 
        }, 1000);
    }    
}

But I am getting error as Uncaught ReferenceError: method is not defined M979:192 C VM979:192 (anonymous function)

function C(p,q) {
    .........
}

I have read many blogs related to setTimeOut and discovered this way to do it.

My code:

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();
}
도움이 되었습니까?

해결책

These lines are illogically placed:

if(regUserName===undefined || regUserMobile===undefined) {
    setTimeout(function()   {
        openChat(regUserName,regUserName); // Error Here
    }, 1000);
}

Just call openChat in the JSONStore.get() callback:

    regUserMobile=res[0].json.userPass;
    console.log("For Chat Data 1 is "+regUserName+"  "+regUserMobile);

    if(regUserName===undefined || regUserMobile===undefined) {
        openChat(regUserName,regUserName);
    }
})

You also might want to replace === with !===, there. I would imagine that was intended to be a check to see if the parameters are filled.

다른 팁

I tried your code and i got the same error as you has mentioned, i have tweeked the code and now its working with this code just modify the URL link as per what you want.

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

I know this is different way and probably not advicable but that is how i fixed it.

I tired this piece and its working correctly:

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top