English 中文(简体)
使用jquery计时事件需要帮助
原标题:Need help timing events with jquery

这是我的代码:

        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毫秒是否通过感应的正确方法是什么?在代码的第5行,我有一个变量“something”,它需要用某种计数器来替换。建议?

最佳回答

您可以实例化一个“Date”对象,如下所示:

var then = new Date();

稍后您可以制作另一个:

var now = new Date();

减法以毫秒为单位给出差值:

var elapsed = now - then;

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

问题回答

下面的代码是未测试的,但基本上,在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;
        }
    }
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签