Skip to content

Instantly share code, notes, and snippets.

@jixunmoe
Created April 27, 2014 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jixunmoe/11356977 to your computer and use it in GitHub Desktop.
Save jixunmoe/11356977 to your computer and use it in GitHub Desktop.
// Better interval
var betterInterval = function (foo, timeout) {
for (var i=2, extraArgs=[], that=this; i<arguments.length; i++)
extraArgs.push (arguments[i]);
var fooNext = function () {
var args = extraArgs.slice();
args.splice(0, 0, fooNext);
for (var i=0; i<arguments.length; i++)
args.push (arguments[i]);
setTimeout (function () {
foo.apply (that, args);
}, timeout);
};
fooNext ();
};
// Example: interval with custom arguments.
(function () {
var GM_xmlhttpRequest = function (opts) {
console.log (opts.url);
opts.onload ({response: ''});
};
betterInterval(function (doAgain, url, i) {
// 初次调用
if (isNaN (i))
i = 0;
GM_xmlhttpRequest({
method: "GET",
url: url + i,
headers: {
"User-Agent": "Mozilla/5.0",
Accept: "text/xml"
},
onload: function(response) {
msg_reply = response.responseText;
if (++i < 10)
doAgain (i);
}
});
}, 1000, 'http://www.baidu.com/');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment