注:
setTimeout(checkDoScroll, 1); 目的是让浏览器尽快执行 checkDoScroll 函数,间隔时间设置为 1ms,对当下的浏览器来说是不太可能的。每个浏览器都有自己默认的最小间隔时间,即使时间设置为最小间隔时间,也只是代表隔这些时间过去之后, 会把 checkDoScroll 加入到执行队列中,如果此时 进程空闲,则会立即执行该代码。
来自前端乱炖:
2016.11.17更新:看到很多搜索这篇文章进来,最上面的代码是互联网上比较完善的一个代码,下面附多一个在stackoverflow的高分回答结合上面的代码,给出get和post的两种不同请求方法:
var ajax = {}; ajax.x = function () { if (typeof XMLHttpRequest !== 'undefined') { (); } var versions = [ "MSXML2.XmlHttp.6.0", "MSXML2.XmlHttp.5.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.2.0", "Microsoft.XmlHttp" ]; var xhr; for (var i = 0; i < versions.length; i++) { try { xhr = new ActiveXObject(versions[i]); break; } catch (e) { } } return xhr; }; ajax.send = function (url, method, data, success,fail,async) { if (async === undefined) { async = true; } var x = ajax.x(); x.open(method, url, async); x.onreadystatechange = function () { if (x.readyState == 4) { var status = x.status; if (status >= 200 && status < 300) { success && success(x.responseText,x.responseXML) } else { fail && fail(status); } } }; if (method == 'POST') { x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); } x.send(data) }; ajax.get = function (url, data, callback, fail, async) { var query = []; ) { query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); } ajax.send(url + (query.length ? '?' + query.join('&') : ''), 'GET', null, success, fail, async) }; ajax.post = function (url, data, callback, fail, async) { var query = []; ) { query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); } ajax.send(url,'POST', query.join('&'), success, fail, async) };使用方法:GET
ajax.get('/test.php', {foo: 'bar'}, function(response,xml) { //success }, function(status){ //fail });POST
ajax.post('/test.php', {foo: 'bar'}, function(response,xml) { //succcess },function(status){ //fail });如果觉得这篇对你有帮助有帮助,欢迎点击下面的分享。谢谢