Created
July 6, 2016 00:39
-
-
Save JohnBaek/c6909e104e8deeb32bcb3a402dcfeff5 to your computer and use it in GitHub Desktop.
JS_Jsonp_implement.js
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
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