Created
April 27, 2014 22:17
-
-
Save jixunmoe/11356977 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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