需要使用 jquery 计时事件的帮助

发布于 2024-11-09 16:57:36 字数 805 浏览 0 评论 0原文

这是我的代码:

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

第一次通过此函数检查 100 毫秒是否已通过的正确方法是什么?在代码的第五行中,我有变量“something”,需要用某种计数器替换它。或者也许我正在以完全错误的方式处理这个问题。建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-11-16 16:57:36

您可以像这样实例化一个“Date”对象:

var then = new Date();

稍后您可以创建另一个对象:

var now = new Date();

减法给出以毫秒为单位的差异:(

var elapsed = now - then;

当两个日期值出现在减法的任一侧时,从“Date”到“Number”的强制转换是隐式的转换就像调用“now.getTime()”。)

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

天生の放荡 2024-11-16 16:57:36

以下代码未经测试,但基本上,在 100 毫秒后,它应该将 timeout 重置回 null 并最终将 tripper 设置回 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;
        }
    }
}

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文