javascript - javascript 中的线程

标签 javascript jquery

我有函数 GoToCell(number); 此功能呈现玩家的 Action 。 我有这样的情况出现:这个人调用这个函数两次之前超过了最后一次调用的执行。 一个调用被挂断并从 2 个调用开始运行。我需要调用两个没有工作直到结束是不是怎么执行呢?

最佳答案

您遇到的问题可能与线程无关,而与动画或电影播放有关。很难从问题中分辨出来。

但在这两种情况下,答案都是确保您设置了一个标志,告诉自己动画或任何正在进行中,并在其上使用完成回调来清除该标志。然后,在用户单击小部件或其他任何内容时检查标志,并且不要启动动画的第二个副本等。

同样,没有任何代码或上下文,很难具体提供帮助,所以这里有一个使用 jQuery 动画的示例。我不是说“使用 jQuery”或其他任何东西,只是演示上面列出的概念(设置标志、防止重复激活、使用完成回调):

Live copy | source

HTML:

<input type="button" id="theButton" value="Click to animate">
<div id="target"></div>
<div id="output"></div>

CSS:

#target {
  position: absolute;
  left: 0px;
  top: 3em;
  border: 1px solid black;
  background-color: #00d;
  width: 20px;
  height: 20px;
}
#output {
  margin-top: 6em;
}

JavaScript:

jQuery(function($) {

  var animating = false;
  var direction = "+";

  $("#theButton").click(function() {
    // Are we already animating?
    // (Of course, ideally you'd disable the button or similar)
    if (animating) {
      // Yes, say we're busy
      display("Animation still in progress");
    }
    else {
      // No, do it
      display("Animating...");
      animating = true;
      $("#target").animate({
        left: direction + "100"
      }, 3000, function() {
        // Completion callback, we're not animating anymore
        animating = false;
        display("Done");
      });
      direction = direction === "+" ? "-" : "+";
    }
  });

  function display(msg) {
    $("<p>").html(String(msg)).appendTo("#output");
  }

});

关于javascript - javascript 中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10385020/

相关文章:

javascript - 如何使用appendTo显示信息而不是在警告框中显示信息?

javascript - v2.x 中的 Parsley.js 日期验证

javascript - Bootstrap Carousel 在 Chrome 中不工作

jquery - 将鼠标悬停在图上并更改图标题?

javascript - Flexslider - 通过 ng-repeat 更新图像 - 宽度不正确

javascript - .each() 将属性添加到从 jquery 到 js 的父级

javascript - jquery DOM 遍历失败?

javascript - 调整容器的大小,两个灰色方 block 应该尽可能地增长,受对 Angular 线的约束..简单的方法吗?

javascript - 从下/上向左/右滑动切换

javascript - 我可以在任意 HTML 元素类型上触发 JQuery "change"事件吗?