Skip to content

Instantly share code, notes, and snippets.

@sh19910711
Last active December 10, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh19910711/4351282 to your computer and use it in GitHub Desktop.
Save sh19910711/4351282 to your computer and use it in GitHub Desktop.
$.Callbacksを使う
(function( $ ) {'use strict';
$( function( ) {
/**
* あるAPIからデータを取り出すことができたらcallbacksを発火させる関数
*/
function get_data_from_api( callbacks ) {
var api_url = 'http://judge.u-aizu.ac.jp/onlinejudge/webservice/user?id=shioshiota';
$.get( api_url, function( api_result ) {
var solved = $( 'status', api_result ).first( ).children( 'solved' ).text( ).trim( );
if ( callbacks.fire ) {
callbacks.fire( solved );
}
} );
}
// 実行したい関数を入れておく配列
var func_list = [];
func_list.push( function( api_result ) {
console.log( '@ 実行したい処理 1: api_result = ', api_result );
} );
func_list.push( function( api_result ) {
console.log( '@ 実行したい処理 2: api_result = ', api_result );
} );
// コールバックで呼び出したい関数を登録する
var callbacks = $.Callbacks( );
callbacks.add.apply( null, func_list );
// 非同期な関数を実行する
get_data_from_api( callbacks );
} );
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment