JS我的功能之一似乎不存在

时间:2018-09-19 17:30:20

标签: javascript php jquery

我有一个功能:

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

与我尝试调用它的所有其他功能不同,我得到:Uncaught ReferenceError: postBet is not defined

更新

完整代码:

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

1 个答案:

答案 0 :(得分:0)

结果发现000webhost正在使用网站的缓存版本。它使用的是我的代码的旧版本,但没有该功能。通过删除我的代码并将其重新上传到000webhost来解决该问题。