javascript - setTimeout to JS function inside another one -
i'm looking add settimeout
js function inside function have one, i'm aware can use onclick=settimeout"(foobar(), 2500);"
there's loader inside function, make clear, i'd execute function instantly (show loader div) when button clicked settimout
2500 ms before running $.getjson
. let's want add fake timeout api request because stuff blazing fast.
also, please let me know if loading animation method js ok, think it's lines of code show/hide div. i'm sure there's better way handle this. thanks.
<html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>js loader</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <style type="text/css" id="style"> #myloader { position: relative; left: 50%; top: 50%; z-index: 1; width: 150px; height: 150px; margin: 25% -50; border: 16px solid #000; border-radius: 50%; border-top: 16px solid #3498db; width: 120px; height: 120px; -webkit-animation: spin 2s linear infinite; animation: spin 2s linear infinite; } @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } /* add animation "page content" */ .animate-bottom { position: relative; -webkit-animation-name: animatebottom; -webkit-animation-duration: 1s; animation-name: animatebottom; animation-duration: 1s } @-webkit-keyframes animatebottom { { bottom:-100px; opacity:0 } { bottom:0px; opacity:1 } } @keyframes animatebottom { from{ bottom:-100px; opacity:0 } to{ bottom:0; opacity:1 } } #mydiv { display: none; text-align: center; } </style> <div class="container container-table"> <div class="row vertical-center-row"> <div class="col-md-4 col-md-offset-4"> <h1 id="name" >real-time bitcoin price</h1> <div id="myloader"style="display: none;"></div> <p id="cointime"></p> <div id="dollar"></div> <div id="gbp"></div> <div id="euro"></div><br> <button id="refreshbtn" class="btn btn-primary">load data</button> </div> </div> </div> <script type="text/javascript"> document.getelementbyid("refreshbtn").addeventlistener("click", function () { var x = document.getelementbyid('myloader'); if (x.style.display === 'none') { x.style.display = 'block'; } else { x.style.display = 'none'; } $.getjson("https://api.coindesk.com/v1/bpi/currentprice.json", function (data) { var x = document.getelementbyid('myloader'); if (x.style.display === 'none') { x.style.display = 'block'; } else { x.style.display = 'none'; } $("#cointime").text(data.time.updated); $("#dollar").text("usd : " + ' ' + data.bpi.usd.rate); $("#gbp").text("gbp : " + ' ' + data.bpi.gbp.rate); $("#euro").text("eur :" + ' ' + data.bpi.eur.rate); }) }); </script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> </body> </html>
to delay ajax request wrap $.getjson
call in settimeout()
. note you're using odd mix of jquery , native js functions. i'd suggest using 1 or other, this:
$("#refreshbtn").on("click", function() { $('#myloader').show(); settimeout(function() { $.getjson("https://api.coindesk.com/v1/bpi/currentprice.json", function(data) { $('#myloader').hide() $("#cointime").text(data.time.updated); $("#dollar").text("usd : " + ' ' + data.bpi.usd.rate); $("#gbp").text("gbp : " + ' ' + data.bpi.gbp.rate); $("#euro").text("eur :" + ' ' + data.bpi.eur.rate); }) }, 2500); });
#myloader { position: relative; left: 50%; top: 50%; z-index: 1; width: 150px; height: 150px; margin: 25% -50; border: 16px solid #000; border-radius: 50%; border-top: 16px solid #3498db; width: 120px; height: 120px; -webkit-animation: spin 2s linear infinite; animation: spin 2s linear infinite; } @-webkit-keyframes spin { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); } } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } } /* add animation "page content" */ .animate-bottom { position: relative; -webkit-animation-name: animatebottom; -webkit-animation-duration: 1s; animation-name: animatebottom; animation-duration: 1s } @-webkit-keyframes animatebottom { { bottom: -100px; opacity: 0 } { bottom: 0px; opacity: 1 } } @keyframes animatebottom { { bottom: -100px; opacity: 0 } { bottom: 0; opacity: 1 } } #mydiv { display: none; text-align: center; }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <div class="container container-table"> <div class="row vertical-center-row"> <div class="col-md-4 col-md-offset-4"> <h1 id="name">real-time bitcoin price</h1> <div id="myloader" style="display: none;"></div> <p id="cointime"></p> <div id="dollar"></div> <div id="gbp"></div> <div id="euro"></div><br> <button id="refreshbtn" class="btn btn-primary">load data</button> </div> </div> </div>
also i'd suggest adding 2.5 second delay far much. i'm aware adding slight delay make more obvious data has loaded idea ux, i'd 500ms more enough.
wiki
Comments
Post a Comment