0

If the two variables are equal 6s ==6s, Secdata Images need to load ELSE loading GIF I should show

I'm using MongoDB. My array object

"sec": "6s"
"Secdata" : ["1.jpg", "2.jpg"];

I have four buttons in HTML element

<a href="" data-sec="4s">4s</a>
<a href="" data-sec="6s">6s</a>
<a href="" data-sec="8s">8s</a>
<a href="" data-sec="10s">10s</a>

When I click 6s button. It will take 6s data value and compare with a collection data sec.

     $(document).ready(function() {
         function checkAjax(id, tmax, tid, pid) {
             $.ajax({
                 url: '../tmax',
                 type: 'POST',
                 data: {
                     id: id,
                     tmax: tmax,
                     tid: tid,
                     pid: pid
                 },
                 success: function(response) {
                     $("#sliderA").hide();
                     $("#sliderB").hide();
                     $("#sliderC").hide();
                     $("#tmaxsecsdata").html(response);
                     if (second == tmax) {

                         checkAjax(id, tmax, tid, pid);

                     } else {
                         var loadinggif = "<img src='loader.gif' class='img-responsive'>";
                         $("#tmaxsecsdata").html(loadinggif);
                     }
                 },
                 error: function() {
                     console.log("Error");
                 }
             })
         }

         $(".secs li a").on("click", function(e) {
             e.preventDefault();
             var id = $(this).data("jsonid");
             var tmax = $(this).data("secs");
             var tid = $(this).data("tid");
             var pid = $(this).data("pid");

             checkAjax(id, tmax, tid, pid);
         })

     })

The second variable, I'm getting from the backend

My problem

When clicking my button, I'm getting a variable (6s => it will be anything 4s,8s,10s,4s). Same like that, I'm getting another variable in my collection. Now I have two variable, This two variables I use to compare ('6s' => it will be anything 4s,8s,10s,4s). If the two variables or not equal, The loader will show on my page. An hour after in my collection will get an update and I will get equal variable. Now I need sho the images.

Thanks

Ashok Charu
  • 274
  • 2
  • 23

1 Answers1

1

Do not use setInterval, just call a new ajax from the success callback if the array is empty.

So assuming that tmaxSec is part of the response then

function checkAjax(id, tmax, tid, pid){
  $.ajax({
        url:'../tmax',
        type:'POST',
        data:{id:id,tmax:tmax,tid:tid,pid,pid},
        success: function(response){
            $("#sliderA").hide();
            $("#sliderB").hide();
            $("#sliderC").hide();
            $("#tmaxsecsdata").html(response);

            if (response.tmaxSec.length === 0) {
                   checkAjax(id,tmax,tid,pid);
            }
        },
        error: function(){
            console.log("Error");
        }
    })
}

$(".secs li a").on("click",function(e){

      var id = $(this).data("jsonid");
      var tmax = $(this).data("secs");
      var tid = $(this).data("tid");
      var pid = $(this).data("pid");

      checkAjax(id,tmax,tid,pid);
})
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Without refresh updated data not showing. If i reload the browser then showing – Ashok Charu May 11 '18 at 13:46
  • @AshokCharu there must be something else at play. Are you clicking on multiple `.secs li a` links ? – Gabriele Petrioli May 11 '18 at 14:13
  • @AshokCharu but the code will clear the `$("#tmaxsecsdata")` when any ajax returns `[]` for the `tmaxSec`. So if you click two links one after the other the first might return data but the second might not and it will clear the `#tmaxsecsdata` element – Gabriele Petrioli May 11 '18 at 14:33
  • If the value equal also it's checking multiple time. How to stop checking checkAjax(id,tmax,tid,pid); – Ashok Charu May 15 '18 at 10:16
  • @AshokCharu your original question said to stop checking only if the array object is empty. See https://stackoverflow.com/a/14853974/128165 for how to compare arrays as it is not trivial – Gabriele Petrioli May 15 '18 at 11:15
  • @AshokCharu **do not change the question with a new one**. Create a new question with your new problem and revert this to its previous state. – Gabriele Petrioli May 15 '18 at 12:45
  • but I already created, hereafter I won't create like this, help me with this problem – Ashok Charu May 15 '18 at 12:48