Is there anything I can do to this code to make it more CPU efficient (it is hitting about 80 percent of my CPU now)? I learned javascript yesterday, so it may just be that I am inexperienced. This code controls the transitions of a rather large array of tiles. On mouseover, the tiles flip over and flip back on mouseoff. There will be several threads running at once, I don't see a way around that one. I am using this script because I need to control exactly what the transitions do in ways that webkit-transitions does not support. Hopefully the comments are meaningful enough to shed some light on the code. The function is live because the array of tiles is created in javascript when the page is loaded. After that, there are no more tiles created.
The source can be found here. I don't have a working upload yet. wikisend.com/download/811662/test.zip
Thank you.
//By default, javascript will not complete a hover transition unless the mouse
remains over the entire duration of the transition. This scrip will force the hover
transition to completion.
$(document).ready(function() {
$('.tile').live('mouseenter mouseleave', (function() {
if (event.type == 'mouseover') {
var $this = $(this);
$this.addClass('hover');
//prevents mouseleave from happening when user re-enters after exiting before time is up
$this.data('box-hover-hovered', false);
//tile is not ready for leaving hover state
$this.data('box-hover-not-ready', true);
var timeout = setTimeout(function() {
//box will be ready after time is up
var state = $this.data('box-hover-hovered');
if (state) { //this is entered when user exits before
//time is up
$this.removeClass('hover');
}
$this.data('box-hover-not-ready', false);
//it's ready
}, 400); // .1 second
// Remove previous timeout if it exists
clearTimeout($this.data('box-hover-timeout'));
//stores current timer id (current timer hasn't executed yet)
$this.data('box-hover-timeout', timeout);
}
else {
var $this = $(this);
// If not not-ready, do nothing
// By default, the value is `undefined`, !undefined === true
var not_ready = $this.data('box-hover-not-ready');
if (!not_ready) {
//if user remains hovering until time is up.
$this.removeClass('hover');
} else {
//user would not have completed the action
$this.data('box-hover-hovered', true);
}
}
}));
});