Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get an event's completion time in jquery

hi i am using jquery .

i have a check box click event

jQuery('.btn-check-box').click(function () {

    //code goes here .
});

the problem is the click event takes considerable time to complete.some times the browser gets stuck. so i want to change the code inside the function and reduce the completion time .

now i want to get my current click event completion time . then make changes to the code and again check the completion time .

so i can optimize my code according to the completion time .

is there any way to check an events completion time . please help. thanks in advance.

like image 768
Kanishka Panamaldeniya Avatar asked Feb 18 '23 06:02

Kanishka Panamaldeniya


2 Answers

Pass event as argument.

var createdTime;
jQuery('.btn-check-box').click(function(e){ 
  createdTime = e.timeStamp;
}).done(function(){ calculate time difference });

if you will use console.log(e) then it will show you that jQuery already passing timestamp when event is triggered.

so now you need to find a way when even is getting done so .done() jQuery method will help you.

In side .done() you can also get same e.timestamp and do some math on to get your desired out put in second, minutes or anything.

like image 150
Dipesh Parmar Avatar answered Feb 28 '23 11:02

Dipesh Parmar


Like SrinivasR said, finish time - start time.

jQuery('.btn-check-box').click(function () {
    var t0 = (new Date()).getTime();
    //code goes here
    console.log("Event took",(new Date()).getTime() - t0, "ms to complete.");
});
like image 37
mako-taco Avatar answered Feb 28 '23 12:02

mako-taco