开发者

Need help timing events with jquery

开发者 https://www.devze.com 2023-03-08 19:35 出处:网络
Here\'s my code: tripper = 2; $(\"#topheader\").mousewheel(function(event, delta) { if (tripper == 2){ 开发者_StackOverflow中文版startPlace = $(\"#content\").scrollLeft();

Here's my code:

        tripper = 2;
    $("#topheader").mousewheel(function(event, delta) {
        if (tripper == 2){
            开发者_StackOverflow中文版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?


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


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;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消