Skip to content

Commit 0c88527

Browse files
committed
add options to support specify callback name
1 parent 985dbc4 commit 0c88527

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎src/utils/JSONP.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const JSONP = url => {
1+
const JSONP = (url, options) => {
2+
options = options || {};
3+
url = options.url || url;
24
return new Promise((resolve, reject) => {
35
let timer = 0;
46
let script = document.createElement('script');
5-
const callbackName = `__jsonp_${Date.now()}_callback`;
7+
const callbackName = options.callbackName || `__jsonp_${Date.now()}_callback`;
68
window[callbackName] = (...args) => {
79
window.clearTimeout(timer);
810
document.body.removeChild(script);
@@ -14,7 +16,7 @@ const JSONP = url => {
1416
reject();
1517
window[callbackName] = null;
1618
}, 5 * 60 * 1000); // timeout in 5 min
17-
script.src = url.replace('=?', `=${callbackName}&_=${Date.now()}`);
19+
script.src = url.replace('=?', `=${callbackName}${options.nocache ? ('&_=' + Date.now()) : ''}`);
1820
document.body.appendChild(script);
1921
});
2022
}

0 commit comments

Comments
 (0)