Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Simpler way to write this snippet?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #151041
    Rugg
    Participant

    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 different

    #151059
    __
    Participant

    The 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 of d and start over. Then you assign c the value of 0 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 always 0, and so, never affects the value b + 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.

    #151144
    __
    Participant

    I 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, and c are in a different scope than d.

    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).

    #151194
    __
    Participant

    I’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 use then will be undefined, so that statement will assign the initial value 0. Once we do then = 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;
    }
    
    #151253
    __
    Participant

    then in my example is not a function/method; simply a variable. It fills the same role as the variable c in your original example.

    #151346
    __
    Participant

    Yeah, I probably should’ve used a different name. I was just thinking of “then” vs. “now”.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.