-1

I am getting error

Uncaught SyntaxError: Illegal break statement

on break;, i am want to stop for loop if server return 5. I am using below code:

<script type="text/javascript">
    function getrblstatus(rbl, hash) {
        var rblhosts = [<?php echo $rblhosts ?>];
        var ActiveRbls = rblhosts.length;
        var progress = '1';
        for (i = 0; i < ActiveRbls; i++) {
            ///// Post data to server /////
            $.ajax({
                url: '<?php echo $site_url?>/includes/lib/ajax.php',
                data: {
                    action: 'getrblstatus',
                    for: rbl,
                    hash: hash,
                    rbl_host: rblhosts[i]
                },
                type: 'post',
                success: function (data) {
                    var percent = (progress/ActiveRbls)*100;
                    percent = percent.toFixed(2);
                    if(data == 5) {
                        console.log('Hash not correct');
                        break;
                    }
                    else{
                        $('#rbl_table > tbody').append(data);
                        $('#progressbar').width(percent+'%');
                        $("#progressbar").html(percent+"% Completed");
                        progress ++;
                        continue;
                    }
                }
            });
        }
    }
</script>
LatentDenis
  • 2,839
  • 12
  • 48
  • 99
Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
  • No you cannot use break inside a function . Move the code to some function and use return statement instead of break – Deepak Kumar T P Dec 26 '17 at 09:27
  • Yes, i am using this code inside function whenever it showing error – Shiv Singh Dec 26 '17 at 09:28
  • 1
    Correct indentation and formatting usually assists in finding syntax errors since the problem usually sticks out (in both meanings of the word). Please take some time on your next post to correctly format your code - you might even find the error in the process. – Lix Dec 26 '17 at 09:28
  • use return statement instead of break – Deepak Kumar T P Dec 26 '17 at 09:29
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – hindmost Dec 26 '17 at 09:29
  • 2
    You can't use `break` or `continue` in callback functions, because the loop will already be done before the callbacks are called. – Barmar Dec 26 '17 at 09:29

1 Answers1

1

You cant mix asynchronous code (the AJAX call) with synchronous code (for/break/continue). So you may await the AJAX request:

async function getrblstatus(rbl, hash) {
    var hosts = [<?php echo $rblhosts ?>], progress = 1;
    for (const rbl_host of hosts) {
        const data = await new Promise((success, error) => $.ajax({
            url: '<?php echo $site_url?>/includes/lib/ajax.php',
            data: {
                action: 'getrblstatus',
                for: rbl,
                hash: hash,
                rbl_host
            },
            type: 'post', success

        }));
        var percent = (progress++ / hosts.length) * 100;

        if(data == 5) {
            console.log('Hash not correct');
            break;
        }
        else {
            $('#rbl_table > tbody').append(data);
            $('#progressbar').width(percent+'%');
            $("#progressbar").html(percent+"% Completed");
        }
    }
}

Due to await, the break; is on the for loop level.

LatentDenis
  • 2,839
  • 12
  • 48
  • 99
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151