4

除了 IE,这在任何地方都可以正常工作,当您单击 #doorbell IE 时说 view.knock 为 null 或不是对象并且失败,我猜它与 enableKnock 中的绑定函数有关,并且它没有传递事件数据?

已解决:我将 detectKnock 函数更改为:

detectKnock: function(e) {
    e = e || window.event;
    e.data.knockInput.push({
        time: (new Date()).getTime()
    });

    clearTimeout(e.data.completeId);
    e.data.completeId = setTimeout(e.data.completeKnock, 1200);
    e.preventDefault();
    return false;
},

错误发生在这里:

e.view.knock.knockInput.push({
     time: (new Date()).getTime()
});

HTML

<button id="doorbell">Push Me</button>

Javascript

var knock = Object();
knock = {
    successCallback: '',
    secretKnock: '',
    knockInput: [],
    reject: 0.25,
    averageReject: 0.15,
    fadeTime: 150,
    completeTime: 1200,
    completeId: '',
    lastNow: '',
    clickEventType: ((document.ontouchstart!==null) ? 'click':'touchstart'),

    enableKnock: function (callback, knock) {
        this.successCallback = callback;
        this.secretKnock = knock;
        $('#doorbell').bind(this.clickEventType, this, this.detectKnock);
    },
    disableKnock: function() {
        $('doorbell').unbind(this.clickEventType, this.knock.detectKnock);
        clearInterval(completeId);
    },
    detectKnock: function(e) {
        var now = new Date().getTime();
        e.view.knock.knockInput.push({
            time: (new Date()).getTime()
        });

        clearTimeout(e.view.knock.completeId);
        e.view.knock.completeId = setTimeout(e.view.knock.completeKnock, 1200);
        e.preventDefault();
        return false;
    },
    completeKnock: function() {
        if (this.knock.validateKnock()) {
            this.knock.successCallback();
        }
        this.knock.knockInput = [];
    },
    validateKnock: function() {
        var maxTime = 0;
        var times = [];
        var time;
        var result = true;
        var dist, i;
        var timeDiff, totalDiff;

        if (this.secretKnock.length == this.knockInput.length) {
            for (i = 1; i < this.knockInput.length && result; ++i) {
                time = this.knockInput[i].time - this.knockInput[i - 1].time;
                times.push(time);
                maxTime = Math.max(maxTime, time);
            }

            for (i = 0; i < times.length && result; ++i) {
                timeDiff = Math.abs((times[i] / maxTime) - this.secretKnock[i].delay);
                totalDiff += timeDiff;
                if (timeDiff > this.reject) {
                    result = false;
                }
            }

            if (result && totalDiff / times.length > this.averageReject) {
                result = false;
            }
        } else {
            result = false;
        }
        return result;
    }
};

    // Shave-and-a-haircut, two bits!
    var secret = [
        {delay:0.5},
        {delay:0.25},
        {delay:0.25},
        {delay:0.5},
        {delay:1},
        {delay:0.5},
        {delay:0}
    ];

    knock.enableKnock(allowInside, secret);

    function allowInside() {
        $('#sub_header').remove();
        $('#images').html('<div class="row" id="welcome"><div class="span12"><h1>Welcome to the afterparty.</h1></div></div>');
        $('body').fadeOut(7500);
        setTimeout( function() {  window.location='party.html' }, 1500 );
    }
4

1 回答 1

1

在 IE 8 上,事件 DTO 只是窗口对象的成员,而不是传递给事件侦听器,因此任何尝试调用e返回null

detectKnock: function(e) {
    var now = new Date().getTime();
    e.view.knock.knockInput.push({
        time: (new Date()).getTime()
    });

    clearTimeout(e.view.knock.completeId);
    e.view.knock.completeId = setTimeout(e.view.knock.completeKnock, 1200);
//---^----- error on null access 
    e.preventDefault();
    return false;
}

来自 quirksmode.org:

在 Microsoft 事件访问模型中,有一个特殊的属性 window.event 包含最后发生的事件。

来源

于 2012-09-05T21:38:32.287 回答