Skip to content

Instantly share code, notes, and snippets.

@muzi131313
Created October 27, 2020 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzi131313/2e730d7756e17b84610d8b1cd107236f to your computer and use it in GitHub Desktop.
Save muzi131313/2e730d7756e17b84610d8b1cd107236f to your computer and use it in GitHub Desktop.
判断是否在国内
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