Home › Forums › JavaScript › [Solved] Simpler way to write this snippet?
- This topic is empty.
-
AuthorPosts
-
September 23, 2013 at 1:54 pm #151041RuggParticipant
The following snippet forces events to fully execute without interruption before firing again. It works well with keydown or mouse wheel events, providing a smoother output with out the bubbling of queued events. Works perfect as it stands, but I’m curious if there’s a cleaner approach to writing it… Suggestions are welcome…thanks!
var a = 200; var b = 0; var c = 0; var d = Date.now(); if (d - c < b + a) { return; } c = d;
Note: I’m aware of jQuery’s
.stop()
and.clearQueue()
, but the above snippet is slightly differentSeptember 23, 2013 at 6:31 pm #151059__ParticipantThe following snippet forces events to fully execute without interruption before firing again.
I am not sure what you’re trying to do, here. jQuery’s
.stop()
and.clearQueue()
do completely different things (and neither seem to be what you are describing).var a = 200; var b = 0; var c = 0; var d = Date.now(); // say, 1379985942942 if (d - c < b + a) { return; } c = d;
let’s look at this…
if( 1379985942942 - 0 < 200 +0 ) if( 1379985942942 < 200 ) if( false )
…right?
Is this supposed to be in a loop (might be implied by
c = d
)?if( false ){ return; }
Won’t happen, so you assign
c
the value ofd
and start over. Then you assignc
the value of0
and run the comparison again – you have an infinite loop.…I’m curious if there’s a cleaner approach to writing it
well, the only thing that jump out at me is that
b
is always0
, and so, never affects the valueb + a
in the comparison – you could leave it out.But I think we need a bit more explanation about what you’re actually trying to do.
September 24, 2013 at 9:40 pm #151144__ParticipantI have proper names in my app code… just used the letters for a primitive test case.
Honestly, it makes more sense to have “proper names” in the test case – that’s where it’s going to be looked at. In your production code, things can (should) be minimized.
Also, it makes a lot more sense now that I know
a
,b
, andc
are in a different scope thand
.You’ll notice the code snippet is used in both the right and left test functions. Is there a way to possibly just use it once without have to be redundant?
You could make it into a method (of your
test
object, or it could be a bare function if you want to use it globally).September 25, 2013 at 8:58 am #151194__ParticipantI’m somewhat confused about the updated syntax…specifically
this.then = this.then || 0;
We’re checking to see if
then
is already defined before defining it: so, on the first usethen
will be undefined, so that statement will assign the initial value0
. Once we dothen = now
,then
will have a value, and so that statement will leave it with its own value. It’s similar to doing:if( ! this.then ){ this.then = 0; }
September 25, 2013 at 8:55 pm #151253__Participantthen
in my example is not a function/method; simply a variable. It fills the same role as the variablec
in your original example.September 26, 2013 at 6:00 pm #151346__ParticipantYeah, I probably should’ve used a different name. I was just thinking of “then” vs. “now”.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.