Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help timing events with jquery

Here's my code:

        tripper = 2;
    $("#topheader").mousewheel(function(event, delta) {
        if (tripper == 2){
            startPlace = $("#content").scrollLeft();
            startCounter = something;
            tripper = 1;
        } else {
            currentPlace = $("#content").scrollLeft();
            if(startCounter < 100){ // milliseconds
            distanceMoved = currentPlace - startPlace;
                if(distanceMoved > 100){
                    slideRight();
                } else if(distanceMoved < -100){
                    slideLeft();
                }
            } else {
                tripper = 2;
            }
        }
    }

What is the proper way to check if 100 milliseconds has passed sense the first time through this function? In the 5th line of code i have the variable "something" which needs to be replaced with a counter of some sort. Or maybe I'm going about this in an entirely wrong way. Suggestions?

like image 988
Dave Avatar asked May 25 '11 17:05

Dave


People also ask

What is the use of delay () method in jQuery?

jQuery delay() Method The delay() method sets a timer to delay the execution of the next item in the queue.

How is an event handled in jQuery?

Whenever a jQuery event is fired, jQuery passes an Event Object to every event handler function. The event object provides various useful information about the event.

Why is jQuery used to wire up events?

jQuery makes it straightforward to set up event-driven responses on page elements. These events are often triggered by the end user's interaction with the page, such as when text is entered into a form element or the mouse pointer is moved.

Does jQuery have action after time?

Answer: Use the jQuery delay() method You can use the jQuery delay() method to call a function after waiting for some time. Simply pass an integer value to this function to set the time interval for the delay in milliseconds.


2 Answers

You can instantiate a "Date" object like this:

var then = new Date();

Later you can make another one:

var now = new Date();

Subtraction gives the difference in milliseconds:

var elapsed = now - then;

(The coercion from "Date" to "Number" is implicit when the two date values appear on either side of the subtraction operator. The conversion is just like calling "now.getTime()".)

like image 143
Pointy Avatar answered Oct 13 '22 16:10

Pointy


The following code it is untested but basically, after 100 milliseconds, it should reset timeout back to null and ultimately set tripper back to 2;

tripper = 2;
timeout = null;
$("#topheader").mousewheel(function(event, delta) {
    if (tripper == 2){
        startPlace = $("#content").scrollLeft();
        if (!timeout) {
            setTimeout(function() {
                timeout = null
            }, 100);
        }
        tripper = 1;
    } else {
        currentPlace = $("#content").scrollLeft();
        if(timeout){ // milliseconds
            distanceMoved = currentPlace - startPlace;
            if(distanceMoved > 100){
                slideRight();
            } else if(distanceMoved < -100){
                slideLeft();
            }
        } else {
            tripper = 2;
        }
    }
}
like image 23
Chris Gutierrez Avatar answered Oct 13 '22 17:10

Chris Gutierrez