Sush
Sush

Reputation: 1457

multiple setinterval issue with jquery

I am performing setInterval for following code. On a button click i have to perform below ones one by one

// show VM started

    vmstart = setInterval(function () {
      $.get('/getVMstatus', function (data) {
        if (data != "") {
          clearInterval(vmstart);
          $("#vmstart").show();
          $("#vmstart").html(data[0].ip)+".....</br>";
          $.post('/deleteReport', function (data) {});
        }
      });
    }, 5000);


// get VM IP Address
    inter = setInterval(function () {
      $.get('/getVm', function (data) {
        if (data != "") {
          clearInterval(inter);
          $("#vmipdiv").show();
          $("#vmipdiv").html("VM IP : " + data[0].ip);
          $.post('/deleteReport', function (data) {});
        }
      });
    }, 6000);

// Test start
teststart = setInterval(function () {
      $.get('/geteststatus', function (data) {
        if (data != "") {
          clearInterval(teststart);
          $("#teststart").show();
          $("#teststart").html("Test Starting in "+data[0].ip+"</br>");
          $.post('/deleteReport', function (data) {});
        }
      });
    }, 11000);

what i need in the button click is

first call /getVMstatus only after filling data to html call /getVM after getting this data call /getteststatus how it is possible?

Upvotes: 0

Views: 99

Answers (2)

P.Sethuraman
P.Sethuraman

Reputation: 715

Try done callback

    / show VM started
          $.get('/getVMstatus', function (data) {
            if (data != "") {

              $("#vmstart").show();
              $("#vmstart").html(data[0].ip)+".....</br>";
              $.post('/deleteReport', function (data) {});
            }
          }).done(function(){$.get('/getVm', function (data) {
            if (data != "") {

              $("#vmipdiv").show();
              $("#vmipdiv").html("VM IP : " + data[0].ip);
              $.post('/deleteReport', function (data) {});
            }
          }).done(function(){
$.get('/geteststatus', function (data) {
            if (data != "") {

              $("#teststart").show();
              $("#teststart").html("Test Starting in "+data[0].ip+"</br>");
              $.post('/deleteReport', function (data) {});
            }
          });
});
});

Upvotes: 1

Snehal S
Snehal S

Reputation: 875

You can call one function inside another

$.get('/getVMstatus', function (data) {
    if (data != "") {
        $("#vmstart").show();
        $("#vmstart").html(data[0].ip)+".....</br>";
        $.post('/deleteReport', function (data) {
            $.get('/getVm', function (data) {
                if (data != "") {
                    $("#vmipdiv").show();
                    $("#vmipdiv").html("VM IP : " + data[0].ip);
                    $.post('/deleteReport', function (data) {
                        $.get('/geteststatus', function (data) {
                            if (data != "") {
                                $("#teststart").show();
                                $("#teststart").html("Test Starting in "+data[0].ip+"</br>");
                                $.post('/deleteReport', function (data) {});
                            }
                        });
                    });
                }
            });
        });
    }
});

Upvotes: 1

Related Questions