If you just want to add a timeout, you can add it to the first function in three places:
setTimeout(function() {callback(this.responseText, params)}, 1000)
And your callback will complete in about 1 second. The second step is the second callback call.
The third place I would suggest is to wrap this function as shown above:
ajaxCallBack(url, function () { if (this.readyState == 4 && this.status == 200) { if (callback) { if (params) { callback(this.responseText, params); } else { callback(this.responseText); } } } });
Usually, when I connect to testing my Internet connection, I rather add throttling to chrome creation tools, such as:

Here is your code with the first approach:
AJAX = function (url, callback, params) { var dt = new Date(); url = (url.indexOf('?') == -1) ? url + '?_' + dt.getTime() : url + '&_' + dt.getTime(); if (url.indexOf('callback=') == -1) { ajaxCallBack(url, function () { if (this.readyState == 4 && this.status == 200) { if (callback) { if (params) { console.log(new Date()); setTimeout(function() {callback(this.responseText, params)}, 2000); } else { console.error((new Date()).getSeconds()); setTimeout(function() {callback(this.responseText)}, 2000); } } } }); } else { var NewScript = d.createElement("script"); NewScript.type = "text/javascript"; NewScript.src = url + '&_' + Math.random(); d.getElementsByTagName('head')[0].appendChild(NewScript); } }, ajaxCallBack = function (url, callback) { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = callback; xmlhttp.open("GET", url, true); xmlhttp.send(); } AJAX('http://ip.jsontest.com/', function() {console.error((new Date()).getSeconds()); });
source share