0

I have a function :

var postBet = function(enemyID, ip_1, ip_2, playerID) {
    $.post("save.php", {
            enemyID: enemyID,
            ip_1: ip_1,
            ip_2: ip_2,
            playerID: playerID
        },
        function(data, status) {
            document.getElementById("saveWarningText").innerHTML = data;
            $("#saveWarningText").fadeIn(100);
            setTimeout(function() {
                $("#saveWarningText").fadeOut(100);
            }, 3000);
        }
    );
};

Unlike all of my other functions when I attempt to call it I get: Uncaught ReferenceError: postBet is not defined

Update

Full code:

var cors_api_url = 'https://cors-anywhere.herokuapp.com/';

var doCORSRequest = function(options, printResult) {
    var x = new XMLHttpRequest();
    x.open(options.method, cors_api_url + options.url);
    x.onload = x.onerror = function() {
        printResult(
            (x.responseText || '')
        );
    };
    if (/^POST/i.test(options.method)) {
        x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    x.send(options.data);
};

var playerIDs = [""];
var playerNames = [""];
var playerScores = [""];

var enemyIDs = [""];
var enemyNames = [""];
var enemyScores = [""];

var parser2 = new DOMParser();
var xmlDoc2;

var done1 = false;
var done2 = false;

var step1 = function() {

    for (var i = 0; i < playerIDs.length; i++) {
        var url2 = "http://www" + document.getElementById('extension').value + ".myfantasyleague.com/" + (new Date()).getFullYear() + "/export?TYPE=playerScores&L=" + document.getElementById('code').value + "&RULES&PLAYERS=";
        url2 = url2 + playerIDs[i] + ",";
        var out2 = "";

        callback1(i, url2);
        if (i >= playerIDs.length - 1) {
            done1 = true;
        }
    }

    for (var i = 0; i < enemyIDs.length; i++) {
        var url2 = "http://www" + document.getElementById('extension').value + ".myfantasyleague.com/" + (new Date()).getFullYear() + "/export?TYPE=playerScores&L=" + document.getElementById('code').value + "&RULES&PLAYERS=";
        url2 = url2 + enemyIDs[i] + ",";
        var out2 = "";

        callback2(i, url2);
        if (i >= playerIDs.length - 1) {
            done2 = true;
        }
    }

    if (done1 && done2) {
        step2();
    }
};

var callback1 = function(i, url2) {
    doCORSRequest({
        method: this.id === 'post' ? 'POST' : 'GET',
        url: url2,
    }, function printResult(result) {
        out2 = result;
        xmlDoc2 = parser2.parseFromString(out2, "text/xml");
        var temp = 0;
        for (var j = 0; j < parseInt(xmlDoc2.getElementsByTagName('playerScore').length) - 1; j++) {
            if (xmlDoc2.getElementsByTagName('playerScore')[j].getAttribute('score') !== "") {
                temp = temp + parseInt(xmlDoc2.getElementsByTagName('playerScore')[j].getAttribute('score'));
            }
            playerScores[i] = temp;
        }

    });
};

var callback2 = function(i, url2) {
    doCORSRequest({
        method: this.id === 'post' ? 'POST' : 'GET',
        url: url2,
    }, function printResult(result) {
        out2 = result;
        xmlDoc2 = parser2.parseFromString(out2, "text/xml");
        var temp = 0;
        for (var j = 0; j < parseInt(xmlDoc2.getElementsByTagName('playerScore').length) - 1; j++) {
            if (xmlDoc2.getElementsByTagName('playerScore')[j].getAttribute('score') !== "") {
                temp = temp + parseInt(xmlDoc2.getElementsByTagName('playerScore')[j].getAttribute('score'));
            }
            enemyScores[i] = temp;
        }

    });
};

var step2 = function() {
    for (var i = 0; i < playerIDs.length; i++) {
        var url2 = "http://www" + document.getElementById('extension').value + ".myfantasyleague.com/" + (new Date()).getFullYear() + "/export?TYPE=players&PLAYERS=";
        url2 = url2 + playerIDs[i];

        callback3(i, url2);
    }

    for (var i = 0; i < enemyIDs.length; i++) {
        var url2 = "http://www" + document.getElementById('extension').value + ".myfantasyleague.com/" + (new Date()).getFullYear() + "/export?TYPE=players&PLAYERS=";
        url2 = url2 + enemyIDs[i];

        callback4(i, url2);
    }
};

var callback3 = function(i, url2) {
    doCORSRequest({
        method: this.id === 'post' ? 'POST' : 'GET',
        url: url2
    }, function printResult(result) {
        xmlDoc2 = parser2.parseFromString(result, "text/xml");
        playerNames[i] = xmlDoc2.getElementsByTagName('player')[0].getAttribute('name');
        var option = document.createElement("option");
        var node = document.createTextNode(playerNames[i] + " : " + playerScores[i]);
        option.appendChild(node);

        var element = document.getElementById("you");
        element.appendChild(option);
    });
};

var callback4 = function(i, url2) {
    doCORSRequest({
        method: this.id === 'post' ? 'POST' : 'GET',
        url: url2
    }, function printResult(result) {
        xmlDoc2 = parser2.parseFromString(result, "text/xml");
        enemyNames[i] = xmlDoc2.getElementsByTagName('player')[0].getAttribute('name');
        var option = document.createElement("option");
        var node = document.createTextNode(enemyNames[i] + " : " + enemyScores[i]);
        option.appendChild(node);

        var element = document.getElementById("enemy");
        element.appendChild(option);
    });
};

var postBet = function(enemyID, ip_1, ip_2, playerID) {
    $.post("save.php", {
            enemyID: enemyID,
            ip_1: ip_1,
            ip_2: ip_2,
            playerID: playerID
        },
        function(data, status) {
            document.getElementById("saveWarningText").innerHTML = data;
            $("#saveWarningText").fadeIn(100);
            setTimeout(function() {
                $("#saveWarningText").fadeOut(100);
            }, 3000);
        }
    );
};

(function() {
    postBet('1', '1', '1', '1');
    document.getElementById('start').onclick = function(e) {
        var url = "http://www" + document.getElementById('extension').value + ".myfantasyleague.com/" + (new Date()).getFullYear() + "/export?TYPE=rosters&L=" + document.getElementById('code').value;
        var data = "";
        var output = "";
        var parser = new DOMParser();
        var xmlDoc;

        var n1 = document.getElementById("you");
        while (n1.firstChild) {
            n1.removeChild(n1.firstChild);
        }

        var n2 = document.getElementById("enemy");
        while (n2.firstChild) {
            n2.removeChild(n2.firstChild);
        }

        e.preventDefault();

        doCORSRequest({
            method: this.id === 'post' ? 'POST' : 'GET',
            url: url,
            data: data
        }, function printResult(result) {
            output = result;
            xmlDoc = parser.parseFromString(output, "text/xml");
            for (var i = 1; i < xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('team').value) - 1].childNodes.length; i += 2) {
                playerIDs.length = Math.round((xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('team').value) - 1].childNodes.length / 2) - 1);
                playerScores.length = Math.round((xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('team').value) - 1].childNodes.length / 2) - 1);
                playerIDs[Math.round(i / 2) - 1] = xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('team').value) - 1].childNodes[i].getAttribute('id');
            }

            for (var i = 1; i < xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('other').value) - 1].childNodes.length; i += 2) {
                enemyIDs.length = Math.round((xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('other').value) - 1].childNodes.length / 2) - 1);
                enemyIDs[Math.round(i / 2) - 1] = xmlDoc.getElementsByTagName("franchise")[parseInt(document.getElementById('other').value) - 1].childNodes[i].getAttribute('id');
            }
            step1();
        });
    };
})();
Cal W
  • 167
  • 1
  • 14
  • 2
    Where exactly is the function declared? Inside a "load" or "ready" handler? How are you trying to call it? – Pointy Sep 19 '18 at 17:32
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Taplar Sep 19 '18 at 17:34
  • The function is declared just in my javascript file. It is not inside any handler. I have tried calling it both inside of another function that does work, and in the javascript console. EDIT: added full code – Cal W Sep 19 '18 at 17:40
  • From the snippet you gave it looks like it is scoped globally, and you are trying to call it after it is defined, so that should be fine. But you said if you open your console and type `postBet` it doesn't recognize it or try to auto complete it? – Taplar Sep 19 '18 at 17:45
  • Yes @Taplar that is correct. Oddly, when it works when running from my HTML file directly, but when I run on the version hosted on 000webhost it doesn't work. The files are exactly correct, I can confirm. You can check out my hosted version [here](https://ffcompare.000webhostapp.com/index.html) – Cal W Sep 19 '18 at 17:48
  • Your main.js file is empty. I don't see that code on or being included on, that page – Taplar Sep 19 '18 at 17:51
  • Refreshed my 000webhost page, and main.js was empty. After updating main.js once again, the version displayed on the website has not changed. Looks like 000webhost is using an old cached version of the website. – Cal W Sep 19 '18 at 17:54
  • Hit Ctrl+F5 to force reload and I see the file now, and it recognizes the method in the console. – Taplar Sep 19 '18 at 17:55
  • Yes, I had to re-upload all of my code to 000webhost to fix the issue. – Cal W Sep 19 '18 at 17:58

1 Answers1

0

Turns out 000webhost was using a cached version of the website. It was using an older version of my code without that function. Fixed the problem by deleting and re-uploading my code to 000webhost.

Cal W
  • 167
  • 1
  • 14