0

In my jQuery function, the items in each table row are downloaded via the following method. Instead of working on each line, the color change command made in the success section works after all processes are finished. What should I do to make it work after each download? I think this is due to Thread.Sleep (1000). but the site where I downloaded the file wants 1 second between each request.

here is my qjuery function

        $.each($("tr[class='sec']"), function () {
        if ($(this).children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {
            durum = $(this);
            var beyan = {};
            var secililer = [];
            beyan.Id = $(this).attr("id");
            beyan.TahakkukId = $(this).data("id");
            beyan.KisaKod = $(this).children('td:eq(2)').html();
            beyan.BeyannameTuru = $(this).children('td:eq(4)').html();
            beyan.Ay = $(this).children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = $(this).children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                    Indirildi(durum);

                }
            });


        }
    });
function Indirildi(gelen) {
    gelen.css('background-color', 'green');
    gelen.children('td:eq(8)').html("İndirildi");
}

here is posted controller

 public async Task<string> BelgeAl(List<Beyanname> secililer, List<Islem> islemler, string token)
{
    bool indir = true;
    bool yazdir = false;
    bool gonder = false;
    foreach (var islem in islemler)
    {
        if (islem.IslemTuru == "cbyazdir")
        {
            yazdir = true;
        }
        if (islem.IslemTuru == "cbgonder")
        {
            gonder = true;
        }
    }
    foreach (var GelenBeyan in secililer)
    {
        string YolAdi = YolHazirla(GelenBeyan);
        string DosyaAdi = DosyaAdiHazirla(GelenBeyan);

        await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Beyan", GelenBeyan.Id, "");
        await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Tahakkuk", GelenBeyan.Id, GelenBeyan.TahakkukId);

    }
    return "";
}

and here is my download async method

        public async System.Threading.Tasks.Task<Boolean> dosyaindir(string token, string yol, string tur, string id, string thkid)
    {

        string URI = "https://ebeyanname.gib.gov.tr/dispatch?";
        string myParameters = "cmd=IMAJ&subcmd=";
        if (tur == "Beyan")
        {
            myParameters += "BEYANNAMEGORUNTULE";
        }
        else
        {
            myParameters += "TAHAKKUKGORUNTULE&tahakkukOid=" + thkid;
        }
        myParameters += "&TOKEN=" + token;
        myParameters += "&beyannameOid=" + id;
        myParameters += "&inline=true";
        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(string.Concat(URI + myParameters));
        Req.CookieContainer = Cc;
        Req.Method = "GET";
        Req.ContentType = "application/pdf;charset=ISO-8859-9";
        HttpWebResponse Res = (HttpWebResponse)Req.GetResponse();
        Req.KeepAlive = false;
        Req.ProtocolVersion = HttpVersion.Version11;
        Req.ServicePoint.ConnectionLimit = 1;
        Stream dosya = Res.GetResponseStream();
        yol += tur + ".pdf";
        using (var fs = System.IO.File.Create(yol))
        {
            dosya.CopyTo(fs);
        }
        Res.Close();
        Req.Abort();
        Thread.Sleep(1000);     
        return true;
    }

UPDATE i just now try remove

Thread.Sleep(1000); 

and use this function but nothing change

 $.each($("tr[class='sec']"), function () {
        if ($(this).children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {
            durum = $(this);
            var beyan = {};
            var secililer = [];

            setTimeout(function () {

            }, 1000);




            beyan.Id = $(this).attr("id");
            beyan.TahakkukId = $(this).data("id");
            beyan.KisaKod = $(this).children('td:eq(2)').html();
            beyan.BeyannameTuru = $(this).children('td:eq(4)').html();
            beyan.Ay = $(this).children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = $(this).children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                   durum.css('background-color', 'green');
                   durum.children('td:eq(8)').html("İndirildi");

                }
            });


        }
    });
trkmml
  • 34
  • 7
  • the question is what is gelen in your js? is that supposed to be the current row? – Glynn Hurrell May 02 '20 at 14:02
  • yes sorry, i try to use a function i forget to change before past `function Indirildi(gelen) { gelen.css('background-color', 'green'); gelen.children('td:eq(8)').html("İndirildi"); }` and success function is `Indirildi(durum);` – trkmml May 02 '20 at 14:25
  • can i wait this process in each function and delete `Thread.Sleep(1000);` – trkmml May 02 '20 at 14:26
  • yes you can use setTimeout(function() {} ,500) where 500 is the number of milliseconds – Glynn Hurrell May 02 '20 at 14:27
  • but I'm not sure that will necessarily solve it – Glynn Hurrell May 02 '20 at 14:28
  • can you update your question to include the extra js? – Glynn Hurrell May 02 '20 at 14:28
  • i update my question – trkmml May 02 '20 at 14:34
  • Im just having a look now. Just fyi, you need to put everything in the settimeout function, or it won't wait – Glynn Hurrell May 02 '20 at 14:39
  • The next thing I would do is put a console.log in the success method to see how many times it is being called, if at all – Glynn Hurrell May 02 '20 at 14:42
  • i try this, but nothing change, css changes once aplly when each function over, i upload 2 videos for this on here: 1-there is i put everything in settimeout function but substring error. [link](https://youtu.be/gaWO3LUVT2k) 2- there is wait properly each each function but all css change happend at one time [link](https://youtu.be/85W3KGhdIyE) can you check? – trkmml May 02 '20 at 16:18

1 Answers1

0

I find my solution in here i remove each function, and put code in a function with SetTimeOut

 (function IslemYap() {
        i++;
        satir = $("tr[class='sec']:eq(" + i + ")");
        if (satir.children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {                
            sayi++;
            var beyan = {};
            var secililer = [];
            beyan.Id = satir.attr("id");
            beyan.TahakkukId = satir.data("id");
            beyan.KisaKod = satir.children('td:eq(2)').html();
            beyan.BeyannameTuru = satir.children('td:eq(4)').html();
            beyan.Ay = satir.children('td:eq(5)').html().substring(8, 10);
            beyan.Yil = satir.children('td:eq(5)').html().substring(11, 16);
            secililer.push(beyan);
            $.ajax({
                url: '/Ebeyan/BelgeAl',
                type: "POST",
                dataType: "text",
                async: false,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ secililer, islemler, token, }),
                success: function (data) {
                    console.log(sayi);
                    satir.css('background-color', 'green');
                    satir.children('td:eq(8)').html(data);
                    $("#pb").width(pbdurum + "%")
                    pbdurum += oran;
                }
            });
        }
        if (i < toplamsayi) {
            setTimeout(IslemYap, 100);
        }

    })();
trkmml
  • 34
  • 7