简体   繁体   中英

How to alert time process using jQuery in the proper way?

I want to show the time of process of my php code using jQuery


My php code example :

<?php 
   //some queries to database
?>

Here's my jQuery:

$.ajax({
        type: "POST",
        url: "action.php",
        data: {
            action: "simpanFitur", fitur: $('#feature :selected').val()
        },
        success: function(){
            var stop = new Date().getTime()-start;
            alert("Tersimpan.\n Lama proses : " + stop);
        },
        error: function(cek){
            alert("Error");
        }
    });

The alert show this :

在此处输入图片说明

Can somebody help me how to make it more readable ? Because I dont know what value that showed on my alert. Thank you.

I've modified my ajax request to be like this :

$.ajax({
        type: "POST",
        url: "action.php",
        data: {
            action: "simpanFitur", fitur: $('#feature :selected').val()
        },
        success: function(time){
            alert("Tersimpan.\nLama proses : " + time);
        },
        error: function(){
            alert("Error");
        }
    });

But the alert didn't show the time value.

在此处输入图片说明

Did I do something wrong ? Please tell me.

This is the latest alert :

在此处输入图片说明

How to make it more simple ?

You could always return in the action.php response a key called time containing the total time the request took, then you can handle this in your success section and display as you wish.

You could generate the value of time with:

$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM