Created
October 27, 2020 11:40
-
-
Save muzi131313/2e730d7756e17b84610d8b1cd107236f to your computer and use it in GitHub Desktop.
判断是否在国内
This file contains hidden or 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 isInChina(cb) { | |
return new Promise((resolve, reject) => { | |
try { | |
let url = "//graph.facebook.com/feed?callback=h" | |
let xhr = new XMLHttpRequest() | |
let called = false | |
xhr.open("GET", url) | |
xhr.onreadystatechange = function() { | |
if (xhr.readyState === 4 && xhr.status === 200) { | |
called = true | |
resolve(false) | |
} | |
} | |
xhr.send() | |
// timeout 1s, this facebook API is very fast. | |
setTimeout(function() { | |
if (!called) { | |
xhr.abort() | |
resolve(true) | |
} | |
}, 1000) | |
} catch (e) { | |
console.error("fqtest", e) | |
resolve(true) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment