jcubic / jquery.terminal

jQuery Terminal Emulator - JavaScript library for creating web-based terminals with custom commands
https://terminal.jcubic.pl
MIT License
3.12k stars 570 forks source link

Correct way of doing a loop - multiple ajax requests #64

Closed poetawd closed 11 years ago

poetawd commented 11 years ago

Hello,

I dont know if this is a issue with Terminal or I am doing something wrong....

I am using Terminal in my program, and I made a command that will get AJAX data from PHP.

I tested this on chrome and firefox... it works fine in firefox, but in chrome it wont work....

I think that there is something about the ajax: async: false...

Anyway, is there a way to make it work in chrome ?

        else if (command == 'fetch_prices') {

            for (var i=0; i<2; i++)
            { 
                fetchPrices('rtr',274);
                fetchPrices('avr',244);
                fetchPrices('isd',264);
                fetchPrices('dka',158);
                fetchPrices('nph',175);
                fetchPrices('mbs',155);
                fetchPrices('som',249);
                fetchPrices('roe',248);
                fetchPrices('wwk',145);
                fetchPrices('zen',269);

                fetchPrices('m13',249);
                fetchPrices('m12',249);             
                fetchPrices('m11',249);
            }

        }
    function fetchPrices(abreviacao,quantidadecards) {

        var qntCards = quantidadecards;

        term.echo( 'Inicializando sequência de atualização de preço de cards...' );

        term.echo( 'Set: '+abreviacao );
        console.log('Set: '+abreviacao );
        term.echo( 'Quantidade de Cards: '+qntCards );
        console.log('Quantidade de Cards: '+qntCards);

        var i;

        for (i=qntCards; i>0; i--)
        {
            term.echo(' ');
            term.echo( '... Atualizando preço do card '+i+' do set '+ abreviacao +' ...' );
            term.echo(' '); 

            $.ajax({
                type: "GET",
                //data: { set: abreviacao, nmCard: i, <?php echo $this->security->get_csrf_token_name() ?>: "<?php echo $this->security->get_csrf_hash() ?>" },
                url: "cards/getCardPrice/" + abreviacao + "/" + i,
                dataType: "json",
                async: false,
                success: function(data) {

                    if (data.sucesso == true) { //Se for retornada uma resposta de sucesso

                        term.echo('|======================================================================');
                        term.echo('| Preço Atualizado: '+data.nome+' - ' + abreviacao);
                        term.echo('|======================================================================');
                        term.echo('|');
                        if(data.tcgplayer.sucesso) term.echo('|  tcgplayer.com : '+data.tcgplayer.mid);
                        if(data.ugcardshop.sucesso) term.echo('|  ugcardshop.com.br : '+data.ugcardshop.normal);
                        if(data.mtgmintcard.sucesso) term.echo('|  mtgmintcard.com : '+data.mtgmintcard.engnorm);
                        term.echo('|');
                        term.echo('|======================================================================');

                    } else { //Se for retornada uma resposta de sucesso

                        term.echo( 'Erro ao atualizar preço do card...' );

                    }

                    if(data.mensagem){
                        term.echo( data.mensagem );
                    }
                },
                complete: function() {

                }

            });

        }

    };
jcubic commented 11 years ago

What you mean by "not working"? do you got any error? can you explain?

poetawd commented 11 years ago

It doesnt write anything to the console in chrome.... not until all ajax requests are complete..... it works fine in FIREFOX

jcubic commented 11 years ago

I think it's not related to terminal, you should ask that on stackoverflow