2017-08-22 22 views
1

setTimeoutをJS関数に追加しようとしていますが、この関数の中に別のものがあります。私はonclick=setTimeout"(fooBar(), 2500);"を使用できますが、私の関数内にはローダーがあります。ボタンをクリックしてsetTimout〜2500 ms前にこの機能を即座に実行したい(ローダのdivを表示する)ようにしたい。$.getJSONを実行する。そのようなものが急に燃えているので、私は偽のtimeOutをAPI要求に追加したいとしましょう。setTimeout to JS function to another

また、JSでの読み込みアニメーションの方法が問題ないかどうか教えてください。実際にはdivを表示/非表示するにはコードが多すぎると思います。このようなことを処理するには良い方法があると確信しています。ありがとう。

<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 to "page content" */ 
 
.animate-bottom { 
 
    position: relative; 
 
    -webkit-animation-name: animatebottom; 
 
    -webkit-animation-duration: 1s; 
 
    animation-name: animatebottom; 
 
    animation-duration: 1s 
 
} 
 

 
@-webkit-keyframes animatebottom { 
 
    from { bottom:-100px; opacity:0 } 
 
    to { 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>

+0

いいえ、あなたは 'のonclick = setTimeoutを行うことができない "(​​fooBarには()、2500); 'これ、あなたが'しなければならないのonclick = setTimeoutメソッド"(fooBarに、2500年) ;つまり、最初の引数は 'callback'でなければなりません –

答えて

1

単にsetTimeout()$.getJSON呼び出しをラップするAJAX要求を遅らせること。また、あなたはjQueryとネイティブJS関数の奇妙な組み合わせを使用していることにも注意してください。

$("#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 to "page content" */ 
 

 
.animate-bottom { 
 
    position: relative; 
 
    -webkit-animation-name: animatebottom; 
 
    -webkit-animation-duration: 1s; 
 
    animation-name: animatebottom; 
 
    animation-duration: 1s 
 
} 
 

 
@-webkit-keyframes animatebottom { 
 
    from { 
 
    bottom: -100px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    bottom: 0px; 
 
    opacity: 1 
 
    } 
 
} 
 

 
@keyframes animatebottom { 
 
    from { 
 
    bottom: -100px; 
 
    opacity: 0 
 
    } 
 
    to { 
 
    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>

また、私は2.5秒の遅延を追加することはあまりにも多くあることをお勧めしたい:私は、このような何かをどちらか一方を使用してお勧めしたいです。私は、データがロードされていることをより明白にするために若干の遅延を加えることはUXのための良いアイデアだと認識していますが、500msはそれ以上のものになるでしょう。

+0

ありがとう。私はまだ学んでいると私はJSとJQueryの違いを実際に理解していない。私はいくつかのドキュメントを読む必要があります!私は2500msがあまりにも多いことに同意します。再度、感謝します。 – Blacksun

0

ファースト - オブジェクト/要素:

あなたが複数回使用し、常にキャッシュ要素をする必要があります。手段:必要な場所にアクセスできる変数にオブジェクトを割り当てます。どうして?あなたが好きなだけ頻繁に変数を使うことができるからです。これにより、特定のidまたはclassの要素を何度も探す必要がないため、時間と処理能力が大幅に節約されます。これは私のケースではvar xです。

セカンド - ローダー:

ありjQueryshow()hide()のような簡単なものがありますが、私はternary operationを使用。どうして?それは非常に柔軟性があり、私はそれを知って以来、私はそれを使用しています。だから私はあなたにこれを便利なオプションとして表示したい:-)。

サード - タイムアウト:かなりまっすぐ進む

、あなたが行くあなたのfunctionsetTimeout()で、そこに包みます。ここで

は作業フィドルです:

EDIT:今、あなたは別のfunctionx.style.display行を折り返すと、あなたがコードを再利用することができますし、それを2回記述する必要はありませんので、これを呼び出すことができ、私はデモンストレーションの目的のためにこれはうまくいくはずだと思います。

var x = document.getElementById('myloader'); 
 

 
document.getElementById("refreshBtn").addEventListener("click", function() { 
 

 
    x.style.display = (x.style.display === 'none') ? 'block' : 'none'; 
 

 
    setTimeout(function(){ 
 
    $.getJSON("https://api.coindesk.com/v1/bpi/currentprice.json", function (data) { 
 

 
     x.style.display = (x.style.display === 'none') ? 'block' : '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); 
 
    }); 
 
    },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 to "page content" */ 
 
.animate-bottom { 
 
    position: relative; 
 
    -webkit-animation-name: animatebottom; 
 
    -webkit-animation-duration: 1s; 
 
    animation-name: animatebottom; 
 
    animation-duration: 1s 
 
} 
 

 
@-webkit-keyframes animatebottom { 
 
    from { bottom:-100px; opacity:0 } 
 
    to { bottom:0px; opacity:1 } 
 
} 
 

 
@keyframes animatebottom { 
 
    from{ bottom:-100px; opacity:0 } 
 
    to{ 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> 
 

 

 

 
<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 src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

0
var timeout = null 

function refresh() { 
    function load() { 
    $.getJSON('https://api.coindesk.com/v1/bpi/currentprice.json', function (data) { 
     $('#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) 
     timeout = null 
    }) 
    } 

    if (timeout) { 
    clearTimeout(timeout) 
    } 

    timeout = setTimeout(load, 2500) 
} 

document.getElementById('refreshBtn').addEventListener('click', refresh)