comparison index.html @ 12:13d740477326

Split off fetcher functions
author nanaya <me@myconan.net>
date Fri, 17 Jun 2016 10:42:55 +0900
parents 1b8c5c3afaf5
children 9f4d926cb9c5
comparison
equal deleted inserted replaced
11:1b8c5c3afaf5 12:13d740477326
126 $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString()); 126 $("#result-paypal").text((Math.round(x * 1.05)).toLocaleString());
127 $("#ok").show(); 127 $("#ok").show();
128 lock(false); 128 lock(false);
129 }; 129 };
130 130
131 var fetchFixer = function() {
132 if (rates.fixer !== undefined) { return; }
133
134 lock(true);
135 locks.fixer = true;
136
137 $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" })
138 .done(function(data) {
139 rates.fixer = parseFloat(data.rates["IDR"]);
140
141 displayResult();
142 })
143 .fail(function() {
144 $("#error").show();
145 })
146 .always(function() {
147 locks.fixer = false;
148 lock(false);
149 });
150 };
151
152 var fetchBni = function() {
153 if (rates.bni !== undefined) { return; }
154
155 lock(true);
156 locks.bni = true;
157
158 $.get("bni-jpyidr.txt")
159 .done(function(data) {
160 if (data === "") {
161 rates.bni = 0;
162 } else {
163 rates.bni = parseFloat(data);
164 }
165
166 displayResult();
167 })
168 .fail(function() {
169 $("#error").show();
170 })
171 .always(function() {
172 locks.bni = false;
173 lock(false);
174 });
175 };
176
131 var action = function(e) { 177 var action = function(e) {
132 e.preventDefault() 178 e.preventDefault()
133 $(".js").hide() 179 $(".js").hide()
134 180
135 var x = input(); 181 var x = input();
136 182
137 if (x === NaN || x === undefined) { return false; }; 183 if (x === NaN || x === undefined) { return false; };
138 184
139 if (rates.fixer === undefined) { 185 fetchFixer();
140 lock(true); 186 fetchBni();
141 locks.fixer = true;
142
143 $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" })
144 .done(function(data) {
145 rates.fixer = parseFloat(data.rates["IDR"]);
146
147 displayResult();
148 })
149 .fail(function() {
150 $("#error").show();
151 })
152 .always(function() {
153 locks.fixer = false;
154 lock(false);
155 });
156 };
157
158 if (rates.bni === undefined) {
159 lock(true);
160 locks.bni = true;
161
162 $.get("bni-jpyidr.txt")
163 .done(function(data) {
164 if (data === "") {
165 rates.bni = 0;
166 } else {
167 rates.bni = parseFloat(data);
168 }
169
170 displayResult();
171 })
172 .fail(function() {
173 $("#error").show();
174 })
175 .always(function() {
176 locks.bni = false;
177 lock(false);
178 });
179 };
180 187
181 displayResult(); 188 displayResult();
182 }; 189 };
183 190
184 $("form").submit(action) 191 $("form").submit(action)