Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.