This is working fine everywhere except for IE, when you click #doorbell IE says view.knock is null or not an object and fails out, I’m guessing it has something to do with the bind function in enableKnock and its not passing event data?
Solved: I changed the detectKnock function to:
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;
},
the error happens here:
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 );
}
On IE 8, event DTOs are only members of the window object, and not passed onto event listeners, hence any attempted invocation on
ereturnsnull:From quirksmode.org:
Source