javascript - setTimeout 到另一个里面的 JS 函数

标签 javascript jquery

我想将 setTimeout 添加到一个 JS 函数中,但在这个函数中我还有另一个函数,我知道我可以使用 onclick=setTimeout"(fooBar(), 2500);" 但我的函数中有一个加载器,所以为了清楚起见,我想在单击按钮时立即执行该函数(显示加载器 div)但是 setTimout到运行 $.getJSON 之前的 2500 毫秒。假设我想向 api 请求添加一个假超时,因为那东西非常快。

另外,请告诉我我用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>

最佳答案

要延迟 AJAX 请求,只需将 $.getJSON 调用包装在 setTimeout() 中即可。另请注意,您使用的是 jQuery 和 native 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 秒的延迟太多了。我知道添加轻微延迟以使数据加载更加明显对于用户体验来说是个好主意,但我认为 500 毫秒绰绰有余。

关于javascript - setTimeout 到另一个里面的 JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819018/

相关文章:

javascript - 如何配置 Google Analytics 以将 URL 参数作为单独的页面进行跟踪

javascript - 在事件选项卡中加载 Morris.js 图表( Bootstrap )

javascript - Jquery 选择父 div 内的 div 元素

javascript - 如何动画列表?

javascript - 使用 HTML/JavaScript 在网页中嵌入画面 View

javascript - jQuery插入页脚时如何定义变量

javascript - 如何更改select2中输入文本的宽度,并且标签应内联显示?

jQuery 不淡入淡出文本?

javascript - Ajax 响应转换

javascript - Webkit 过渡事件