Skip to content

Instantly share code, notes, and snippets.

@arnemart
Created January 29, 2014 09:56
Show Gist options
  • Save arnemart/8684859 to your computer and use it in GitHub Desktop.
Save arnemart/8684859 to your computer and use it in GitHub Desktop.
$.delayLink
(function($) {
$.fn.delayLink = function(fn, options) {
var settings = $.extend(options, {
timeout: 100
});
$(this).on('click', function(e) {
e.preventDefault();
var went = false;
var link = this;
var go = function() {
went = true;
console.log('go');
if (e.crtlKey || e.metaKey) {
window.open(link.href);
} else {
window.location.href = link.href;
}
};
var timer = setTimeout(go, settings.timeout);
fn(function(dont) {
if (dont !== false) {
clearTimeout(timer);
if (went === false) {
go();
}
}
});
});
};
})(this.jQuery);
$('a').delayLink(function(callback) {
$.ajax({
//stuff
}).done(function() {
callback();
});
}, {
timeout: 200
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment