Ondřej Kolman
Ondřej Kolman

Reputation: 29

Javascript promise execution time

I have JS function which basically periodicaly call server and try if it's still online, I would like to show execution time of this function (something like "ping")

function preventOfflinePayment() {

            new Model('orgis.eet').call("pos_check_server_connection",{},undefined, {
                shadow: false,
                timeout: 2000
            })
                .done(function () {
                    $(".next").show();
                    $("#eet-status").css('color', 'limegreen');
                    $("#ping").html("Time: " + something);
                })
                .fail(function () {
                    $(".next").hide();
                    $("#eet-status").css('color', 'red');

                });

            _.delay(function () {
                preventOfflinePayment();
            }, 1000);
        }

I dont know if it's possible to calculate this or not. Can you suggest something?

Upvotes: 1

Views: 865

Answers (1)

Firanolfind
Firanolfind

Reputation: 1605

Maybe something like that:

function preventOfflinePayment() {
    var start = Date.now();
    var stop;
    new Model('orgis.eet').call("pos_check_server_connection",{},undefined, {
        shadow: false,
        timeout: 2000
    })
    .done(function () {
        stop = Date.now();
        var timeout = (stop - start) / 1000; // in ms
        $(".next").show();
        $("#eet-status").css('color', 'limegreen');
        $("#ping").html("Time: " + timeout + ' ms');
     })
     .fail(function () {
        stop = Date.now();
        var timeout = (stop - start) / 1000; // in ms
        $(".next").hide();
        $("#eet-status").css('color', 'red');
        console.log(timeout)
     });

     _.delay(function () {
       preventOfflinePayment();
     }, 1000);
}

Upvotes: 1

Related Questions