0

My question is i want to get total time of ajax request.. I means when i click on button then make a ajax request and start timer and store time in button caption,after ajax request success stop timer...

My problem is

when i click on button call ajax request and after ajax request successfully then timer start.

What i want

I want to start timer before ajax request and stop after ajax request success

My html code

<input class="btn green start_timer" value="Sync" name="btn" type="button">

My js code

$(document).ready(function () {
    var setTimer = null;
    $("body").on('click', '.start_timer', function () {
        var obj = $(this);
        var start = 1;
        setTimer = setInterval(function () {
            start++;
            obj.val(start);
        }, 1000);

        $.ajax({
            type: "POST",
            url: base_url + "timerstart/start/1325",
            async: false,
            success: function (data) {
                clearInterval(setTimer);
            }
        });
        return false
    });
});
Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30

4 Answers4

0

You can use jQuery Global Ajax Event Handlers

Steps:

  1. Use ajaxSend to trigger timer.

    a. Display overlay.
    b. Start the timer function. Use Interval to update timer on every second.

  2. Use ajaxComplete to stop timer.

    a. You can use ClearInterval to stop timer.

  3. Calculate the difference incase you want that value to display after overlay is closed.

Notes:

  1. Note that above mentioned global events will work as expected when there is only one ajax call at any moment of time.
  2. You need to use Global variables to get the values from global events and calculate the difference.
Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
0

try this below updated:

<script>
    $(document).ready(function () {
        var setTimer = null;
        $("body").on('click', '.start_timer', function () {
            StartDispalyingTimer($(this));
        });
        RunAjax = function (ele){
            $.ajax({
                type: "POST",
                async:true,
                url: "index2.php",
                success: function (data) {clearInterval(setTimer);},
                error: function (data) {clearInterval(setTimer);}
            });
        }
        StartDispalyingTimer = function (ele){var start = 1;
            setTimer = setInterval(function () {start++;ele.val((start-1));}, 1000);
            setTimeout(function(){RunAjax(ele);},1000);
        }
    });
  </script>
Soni Vimalkumar
  • 1,449
  • 15
  • 26
0

Try this, its worked for me

$(document).ready(function () {
    var setTimer = null;

    $("body").on('click', '.start_timer', function () {
        var element = $(this);
        displayTimer(element);
    });

    function getData(element){
        $.ajax({
            type: "GET",
            async:true,
            url: "",
            success: function (data) {
               clearInterval(setTimer);
               element.val("Sync");
            },
            error: function (data) {
                 clearInterval(setTimer);
                 element.val("Sync");
            }
        });
    }


    function displayTimer(element){
        var start = 1;
        setTimer = setInterval(function () {
            start++;
            element.val(start);
        }, 1000);

        setTimeout(function(){
            getData(element);
        },2000);
    }
});
-1

You've got async=false in the options for your ajax request. This makes your request synchronous so the execution of the script "hangs" untill the request comes back with a response. You should never use async is false.

Sandhje Bouw
  • 172
  • 1
  • 6