1 (edited by qwertyuiop 2012-10-05 01:26:42)

Topic: Problema su jQuery ajax uzklausa

Sveiki, turiu tokia javascript funkcija:

function funkcija(i)
{
    if (i == 1) { $("#kraunama").ajaxStart(function() { $("#kraunama").html('Informacija kraunama'); }); }
    
    $.ajax(
    {
        cache: false,
        type: "GET",
        url: "url.php",
        success: function(result)
        {
            $("#nauji_duomenys").html(result);
            if (i == 1) { $("#kraunama").html(''); }
        }
    });
    
    if (ciklas != null) clearTimeout(ciklas);
    ciklas = setTimeout("funkcija(2)", 10000);
}

Jeigu funkcijai perduodamas parametras 2, "Informacija kraunama" nera rodoma.
Jeigu perduodu parametra 1, parodo "Informacija kraunama", tada, po 10 sekundziu, turetu buti funkcija vel iskviesta su parametru 2 ir "Informacija kraunama" nerodoma, bet kai funkcija automatiskai iskvieciama, atsiranda "Informacija kraunama" ir daugiau nebedingsta. Kur galetu buti problema?
Dekui.

Re: Problema su jQuery ajax uzklausa

Tai išsivesk parametrą per console.log(i); ir matysi kas ką ir kur gauna ir kurioje vietoje pameta.

MongoDB Certified Developer
MongoDB Certified DBA
Zend Certified Engineer

Warning: count(): Parameter must be an array or an object that implements Countable in /home/pasokime/domains/mysql.lt/public_html/forumas/include/parser.php on line 820

3 (edited by Min2liz 2012-10-05 08:00:50)

Re: Problema su jQuery ajax uzklausa

Problema tame kad kintamasis i Ajax užklausoje neegzistuoja. Naudok globalų kintamajį:

window.i = i;

Pasieksi iš bet kur naudodamas window.i

Pasidomėk jQuery delay

4 (edited by zygis 2012-10-05 09:30:23)

Re: Problema su jQuery ajax uzklausa

Ir šiaip funkcijai $.ajax perduodamas objektas, kurio metodas success atlieka veiksmus su gautais duomenimis, tai labai panašu, kad jo viduje per this galima pasiekti patį objektą. Jei - taip, niekas tau netrukdo įdėti į pateikiamą objetą papildomų properčių. Ar veiks nežinau, čia tik teorija.

function funkcija(i)
{
    if (i == 1) { $("#kraunama").ajaxStart(function() { $("#kraunama").html('Informacija kraunama'); }); }
    
    $.ajax(
    {
        cache: false,
        type: "GET",
        url: "url.php",
        someSecret:i,
        success: function(result)
        {
            $("#nauji_duomenys").html(result);
            if (this.someSecret == 1) { $("#kraunama").html(''); }
        }
    });
    
    if (ciklas != null) clearTimeout(ciklas);
    ciklas = setTimeout("funkcija(2)", 10000);
}
MongoDB Certified Developer
MongoDB Certified DBA
Zend Certified Engineer

5 (edited by qwertyuiop 2012-10-05 21:44:05)

Re: Problema su jQuery ajax uzklausa

function funkcija(i)
{
    if (i == 1) { $("#kraunama").ajaxStart(function() { $("#kraunama").html('Informacija kraunama'); }); }
    
    $.ajax(
    {
        cache: false,
        type: "GET",
        url: "url.php",
        success: function(result)
        {
            $("#nauji_duomenys").html(result);

            if (i == 1) { $("#kraunama").html(i); }

        }
    });
    
    if (ciklas != null) clearTimeout(ciklas);
    ciklas = setTimeout("funkcija(2)", 10000);
}

nesuprantu, kai atspausdinu i ajax funkcijoj, atspausdina toki i koki ir perduodu..

Re: Problema su jQuery ajax uzklausa

Pakeiciau siek tiek koda, bet vistiek ta pati beda. Reiskia ne tame problema, kad ajax funkcija "nemato" i

function funkcija(i)
{
    if (i == 1)
    {
        $("#kraunama").ajaxStart(function() { $("#kraunama").html('Informacija kraunama'); });
        $.ajax(
        {
            cache: false,
            type: "GET",
            url: "url.php",
            success: function(result)
            {
                $("#nauji_duomenys").html(result);
                if (i == 1) { $("#kraunama").html(''); }
            }
        });
    }
    else
    {
        $.ajax(
        {
            cache: false,
            type: "GET",
            url: "url.php",
            success: function(result)
            {
                $("#nauji_duomenys").html(result);
            }
        });
    }
    
    if (ciklas != null) clearTimeout(ciklas);
    ciklas = setTimeout("funkcija(2)", 10000);
}

Re: Problema su jQuery ajax uzklausa

Tu gal geriau pasakyk ką nori padaryti, nes pagal duotą funkciją tai čia kažkoks bardakas. Nes logiškai neišmastau išvis kam reikalingas setTimeout. if ... else išvis praktiškai dubliuojasi.

Dėl to i tai viskas veikia http://jsfiddle.net/qpNEU/4/

Per Firebug pažiūrėk Console.

Re: Problema su jQuery ajax uzklausa

jeigu i == 1, rodomas tekstas "Informacija kraunama", kol ajax uzklausa vykdoma;
jeigu i == 2 niekas nera rodoma kol uzklausa yra vykdoma;