-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrency.js
53 lines (43 loc) · 1.37 KB
/
Currency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 存放所有貨幣資料
var currencies = {}
// 匯率 API 位置
var api_url = "http://www.kimonolabs.com/api/5gwbb852?apikey=Wrog1m1TeyQhJb3LMNuzQrvJ7sTpxlya"
function load() {
// 建立新的 Ajax 請求
var request = new XMLHttpRequest()
// 告訴 request 當網路狀態改變的時候要做什麼
request.onreadystatechange = function() {
// 當載入完成的時候
if (request.readyState == XMLHttpRequest.DONE) {
// 記錄 HTTP status code
console.log(request.status + " " + request.statusText)
if (request.status == 200) {
var data = JSON.parse(request.responseText)
var collection = data.results.collection1
for (var i in collection) {
var item = collection[i]
currencies[item.currency] = item
}
}
}
}
// 用 HTTP GET 打開網址並送出
request.open("GET", api_url)
request.send()
}
function get(currency, base) {
var item = currencies[currency]
if (!item) {
return "ERR!"
} else {
var rate = Number(item.cash_buy)
var t = base / rate
var s = String(t)
var i = s.indexOf(".")
if (i < 0) {
return s
} else {
return s.substr(0, i) + "." + s.substr(i+1, 2)
}
}
}