1

I have function GoToCell(number); This feature renders the motion of the player. I have such a situation occurs: the person calls this function two times before over the last execution of the call. and a call is dropped and begins to run from 2 call. I need to call two did not work until an end is not how to implement it?

6
  • 5
    Threads are not possible in javscript. Commented Apr 30, 2012 at 14:04
  • 7
    people here dont know the meaning of downvote... this site should stop register kids. Commented Apr 30, 2012 at 14:05
  • 5
    @BenniKa: You haven't heard of web workers, then. And of course, JavaScript on servers can be multi-threaded. Commented Apr 30, 2012 at 14:07
  • @simply denis, can you show us the code? goToCell and the bit which calls it. Commented Apr 30, 2012 at 14:08
  • @T.J.Crowder You're right, I haven't heard of web workers. Interesting to know ... . Commented Apr 30, 2012 at 14:08

3 Answers 3

4

The problem you're having probably doesn't have anything to do with threads, but with animation or movie playback. It's hard to tell from the question.

But the answer in both cases is to make sure you set a flag telling yourself that the animation or whatever is in progress, and use a completion callback on that to clear the flag. Then, check the flag when the user clicks the widget or whatever and don't kick off a second copy of the animation/etc.

Again, without any code or context, it's hard to help specifically, so here's an example using a jQuery animation. I'm not saying "use jQuery" or anything, just demonstrating the concepts listed above (setting a flag, preventing duplicate activations, using a completion callback):

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");
  }

});
Sign up to request clarification or add additional context in comments.

Comments

1

You might want to have a look at web workers. https://developer.mozilla.org/En/Using_web_workers

It's the only possibility so far to have thread-like behaviour in javascript. I dont think it is implemented by all browsers, though.

Comments

1

I'm not sure you need workers or thread to do what you are looking for.

IMO Underscore's function for Functions can help you achieving what you are looking for with debounce, throttle or once

If you don't want to use underscore, I suggest you read the source of the library, it's pretty short and well commented.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.