changeset 12:13d740477326

Split off fetcher functions
author nanaya <me@myconan.net>
date Fri, 17 Jun 2016 10:42:55 +0900
parents 1b8c5c3afaf5
children 9f4d926cb9c5
files index.html
diffstat 1 files changed, 48 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/index.html	Fri Jun 17 10:36:02 2016 +0900
+++ b/index.html	Fri Jun 17 10:42:55 2016 +0900
@@ -128,6 +128,52 @@
       lock(false);
     };
 
+    var fetchFixer = function() {
+      if (rates.fixer !== undefined) { return; }
+
+      lock(true);
+      locks.fixer = true;
+
+      $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" })
+      .done(function(data) {
+        rates.fixer = parseFloat(data.rates["IDR"]);
+
+        displayResult();
+      })
+      .fail(function() {
+        $("#error").show();
+      })
+      .always(function() {
+        locks.fixer = false;
+        lock(false);
+      });
+    };
+
+    var fetchBni = function() {
+      if (rates.bni !== undefined) { return; }
+
+      lock(true);
+      locks.bni = true;
+
+      $.get("bni-jpyidr.txt")
+      .done(function(data) {
+        if (data === "") {
+          rates.bni = 0;
+        } else {
+          rates.bni = parseFloat(data);
+        }
+
+        displayResult();
+      })
+      .fail(function() {
+        $("#error").show();
+      })
+      .always(function() {
+        locks.bni = false;
+        lock(false);
+      });
+    };
+
     var action = function(e) {
       e.preventDefault()
       $(".js").hide()
@@ -136,47 +182,8 @@
 
       if (x === NaN || x === undefined) { return false; };
 
-      if (rates.fixer === undefined) {
-        lock(true);
-        locks.fixer = true;
-
-        $.getJSON("https://api.fixer.io/latest", { base: "JPY", symbols: "IDR" })
-        .done(function(data) {
-          rates.fixer = parseFloat(data.rates["IDR"]);
-
-          displayResult();
-        })
-        .fail(function() {
-          $("#error").show();
-        })
-        .always(function() {
-          locks.fixer = false;
-          lock(false);
-        });
-      };
-
-      if (rates.bni === undefined) {
-        lock(true);
-        locks.bni = true;
-
-        $.get("bni-jpyidr.txt")
-        .done(function(data) {
-          if (data === "") {
-            rates.bni = 0;
-          } else {
-            rates.bni = parseFloat(data);
-          }
-
-          displayResult();
-        })
-        .fail(function() {
-          $("#error").show();
-        })
-        .always(function() {
-          locks.bni = false;
-          lock(false);
-        });
-      };
+      fetchFixer();
+      fetchBni();
 
       displayResult();
     };