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?
3 Answers
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):
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");
}
});
Comments
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
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.
goToCelland the bit which calls it.