【问题标题】:jQuery fadeout time confusing with different resultsjQuery淡出时间与不同的结果混淆
【发布时间】:2017-01-09 03:24:28
【问题描述】:

下面是我在面试中被问到的代码。

在这种情况下,开始时间和结束时间有什么区别?

我发现在这里运行它需要 12 秒,而在 this link 中运行它需要 8 秒。 .!

最令人困惑的是,在每个循环中,控制台打印淡出动画时间增加 2 秒,尽管每个 div 总共完成 2 秒。

谁能详细解释一下这里到底发生了什么?

function getMinsSecs() {
  var dt = new Date();
  return dt.getMinutes() + ":" + dt.getSeconds();
}

$("input").on("click", function() {

  $("p").append("Start time: " + getMinsSecs() + "<br />");

  $("div").each(function(i) {
    console.log(1000 * (i * 2));
    $(this).fadeOut(1000 * (i * 2));
  });

  $("div").promise().done(function() {
    $("p").append("End time: " + getMinsSecs() + "<br />");
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>

<input type="text" id="inp">

【问题讨论】:

  • 这里有一个额外的 DIV - 查看开发者工具检查器,“控制台输出” div
  • 哎呀,是的,2个额外的div:p一个在另一个里面
  • 哦,每个 div 从一开始就在褪色,每个 div 都以不同的速度消失……用开发人员工具检查器检查一下,你会看到所有 div 都在一次褪色

标签: javascript jquery promise delay fadeout


【解决方案1】:

我将尝试在您的代码中解释它:

// if there are 5 <div> elements on the page, what will be the difference between the start and end times displayed?

/**
 * This function gets the current date and prints 
 * the minutes and seconds in the following format
 * mm:ss
 */
function getMinsSecs() {
  var dt = new Date();
  return dt.getMinutes() + ":" + dt.getSeconds();
}

/**
 * Here we are adding a click event listener to the
 * input present in the HTML.
 */
$("input").on("click", function() {

  // This line appends to the p present in the HTML
  // the text with the current minutes and seconds (start time)
  // and the a break line.
  $("p").append("Start time: " + getMinsSecs() + "<br />");

  // This line goes through each div present in the HTML fading out each div multiplying it by the i (index).
  $("div").each(function(i) {
      console.log(i);
    console.log(1000 * (i * 2));
    //The fadeout function lasts the amount of milliseconds passed as an argument
    $(this).fadeOut(1000 * (i * 2));
  });

  // This line waits til every function called over
  // the divs end (this is what the promise function does).
  $("div").promise().done(function() {

    // This function is called after all the
    // fadeout calls got executed and prints again
    // the minutes and seconds appending the current minutes
    // and seconds (end time)
    $("p").append("End time: " + getMinsSecs() + "<br />");
  });

});

根据 cmets,第一个项目会立即淡出,因为 i = 0,然后您还有四 (4) 个 div 和 4 * 2 = 8,因此开始时间和结束时间之间的差异将是八 (8) 秒。

希望对你有帮助!

【讨论】:

  • 很好解释..!谢谢。但大问题仍然存在。第 2 个 div 的超时值 = 2 秒,但第 3 个 = 4 秒,第 4 个 = 6 秒,第 5 个 = 第 8 个。所以总计应该是 0+2+4+6+8 = 20 秒..?就是这个问题。
  • @jaromanda 的回答解决了这个问题。 concurrently 这个词是我对这个困惑的回答。
【解决方案2】:

在这里,你会得到两个用于“控制台”输出的奖励 div(一个在另一个里面......这样就解释了 2 x 2 秒的额外时间

在每个循环中,控制台打印淡出动画时间增加 2 秒,尽管每个 div 总共完成 2 秒。

不,第一个 div 需要 0 秒才能淡出
第二个 div 需要 2 秒才能完全淡出
第三个 div 需要 4 秒才能完全淡出
第四个 div 需要 6 秒才能完全淡出
第五个 div 需要 8 秒才能完全淡出

仔细观察,您会发现它们都以不同的速率同时开始衰减

【讨论】:

  • 哇@Jaromanda,我想你已经解决了我在上述问题中提出的所有问题。 concurrently 这个词解决了我的主要疑问。
【解决方案3】:

8 是正确答案。承诺将在 8s 后完成,因为最后一个 div 的最长褪色持续时间是 4*2(s)。 在这个站点上运行的代码 sn-p 有一个错误。它包括另外 2 个不属于您的代码的 div。尝试替换

console.log(1000 * (i * 2));

到这里

console.log($(this));

你将能够知道出了什么问题

【讨论】:

  • 谢谢@harytd,但它只回答了我的一半问题。
  • 你问过“在这种情况下,开始时间和结束时间有什么区别?”答案就在这里承诺会在之后完成8 秒,因为最长的持续时间是最后一个 div 的 4*2(s) Promise 将等到应用到您的 div 的所有代码都执行完毕!
猜你喜欢
  • 1970-01-01
  • 2013-09-29
  • 2019-10-24
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多