简体   繁体   中英

Javascript jQuery closing bracket error

I want my output to be in order like:

console.log("1st 1:", y1, y2, y3, y4);
console.log("1st 2:", y1, y2, y3, y4);
console.log("2nd 1:", y1, y2, y3, y4);
console.log("2nd 2:", y1, y2, y3, y4);
console.log("3rd:", y1, y2, y3, y4);

But I am getting Uncaught TypeError: undefined is not a function in the done statement.

And only be able to see:

1st 1: 5 5 5 5
1st 2: 8 30 236 365

I can't find anything wrong with this code:

    data: (
        function() {

            // Test
            y1 = 5,
            y2 = 5,
            y3 = 5,
            y4 = 5;

            // Ajax is asynchronous
            function doRun() {
                $.ajax({
                    type: "GET",
                    url: "/getTest",
                    success: function(data) {
                        console.log("1st 1:", y1, y2, y3, y4);
                        y1 = data.V1;
                        y2 = data.V2;
                        y3 = data.V3;
                        y4 = data.V4;
                        console.log("1st 2:", y1, y2, y3, y4);
                    }
                });
                return doRun;
            };

            doRun().done(function() {
                console.log("2nd 1", y1, y2, y3, y4);
            }).fail(function() {
                console.log("2nd 2");
            });

            var data = [],
                time = (new Date()).getTime(),
                i;
            for (i = -10; i <= 0; i++) {
                console.log("3rd:", y1, y2, y3, y4);
                data.push({
                    x: time + i * 10,
                    y: 0
                });
            }
            return data;
        }()
    )

What should I do fix this problem and printing everything in order?

function doRun() {
                return $.ajax({
                    type: "GET",
                    url: "/getTest",
                    success: function(data) {
                        console.log("1st 1:", y1, y2, y3, y4);
                        y1 = data.V1;
                        y2 = data.V2;
                        y3 = data.V3;
                        y4 = data.V4;
                        console.log("1st 2:", y1, y2, y3, y4);
                    }
                });
            };

You were returning the wrong thing. You returned doRun -- the same function that was called. doRun does not have a done property. You meant to return the promise from $.ajax .

Your call to doRun() is returning the doRun function object. Does that actually have a done() method defined on it? I'm guessing not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM