I have made a Ajax Like Button. After clicking the like button, it takes around 800ms - 1100 ms to do the following things:
- Open
insertlike.php
page in the background using Jquery - Add the like to database in
insertlike.php
page - Confirm the like using JSON
- Turn the like button color into green.
But Facebook's
and other website's Like button works very fast.
Facebook directly change the like button color on click or it only change after adding the like into database?
This is my code:
index.php
code to make ajax request
$(".insertlike").submit(function(e) {
var data = $(this).serialize();
var url = $(this).attr("action");
var form = $(this);
$.post(url, data, function(data) {
try {
data = JSON.parse(data);
$(form).children("button").html(data.addremove + " Watchlist");
$(form).children("input#addedornotsend").attr("value",data.addedornotsend);
} catch (e) {
console.log("json encoding failed");
return false;
}
});
return false;
});
Code inside insertlike.php
<?php
// Add to Database code
$response = new \stdClass();
$response->addremove = "".$addremove."";
$response->addedornotsend = "".$addedornotsend."";
die(json_encode($response));
Any way to insert the like button speed? Maybe some php cache trick or something like that? I am still newbie.
Edit: This is my server response time speed test: