Last active
December 10, 2015 00:28
-
-
Save sh19910711/4351282 to your computer and use it in GitHub Desktop.
$.Callbacksを使う
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
(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