Fungsi panggilan balik JS
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
<html>
<head>
<script type="text/javascript">
function foo(response) {
var meta = response.meta;
var data = response.data;
console.log(meta);
console.log(data);
}
var script = document.createElement('script');
script.src = 'https://api.github.com?callback=foo';
document.getElementsByTagName('head')[0].appendChild(script);
</script>
</head>
<body>
<p>Open up your browser's console.</p>
</body>
</html>
$ curl https://api.github.com?callback=foo
> /**/foo({
> "meta": {
> "status": 200,
> "X-RateLimit-Limit": "5000",
> "X-RateLimit-Remaining": "4966",
> "X-RateLimit-Reset": "1372700873",
> "Link": [ // pagination headers and other links
> ["https://api.github.com?page=2", {"rel": "next"}]
> ]
> },
> "data": {
> // the data
> }
> })