Skip to content

Instantly share code, notes, and snippets.

@JohnBaek
Created July 6, 2016 00:39
Show Gist options
  • Save JohnBaek/c6909e104e8deeb32bcb3a402dcfeff5 to your computer and use it in GitHub Desktop.
Save JohnBaek/c6909e104e8deeb32bcb3a402dcfeff5 to your computer and use it in GitHub Desktop.
JS_Jsonp_implement.js
var that = {};
that.send = function(src, options) {
var callback_name = options.callbackName || 'callback',
on_success = options.onSuccess || function(){},
on_timeout = options.onTimeout || function(){},
timeout = options.timeout || 10; // sec
var timeout_trigger = window.setTimeout(function(){
window[callback_name] = function(){};
on_timeout();
}, timeout * 1000);
window[callback_name] = function(data){
window.clearTimeout(timeout_trigger);
on_success(data);
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}
return that;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment