Team:Queens Canada/Javascript/Mousehold

From 2012.igem.org

(Difference between revisions)
(Created page with "/** * jQuery mousehold plugin - fires an event while the mouse is clicked down. * Additionally, the function, when executed, is passed a single * argument representing the cou...")
 
(One intermediate revision not shown)
Line 1: Line 1:
/**
/**
-
  * jQuery mousehold plugin - fires an event while the mouse is clicked down.
+
*
-
  * Additionally, the function, when executed, is passed a single
+
* Modified by Andrew Spode Miller (justfdi.com, @spode) on 9th December 2009 to include a "Delay" parameter.
-
* argument representing the count of times the event has been fired during
+
*
-
* this session of the mouse hold.
+
*/
 +
 
 +
/**
 +
  * jQuery mousehold plugin - fires an event while a mouse click is held down, at a variable interval.
 +
*
 +
  * Additionally, the function is passed a count of times the event has been fired in this session of mousehold.
  *
  *
  * @author Remy Sharp (leftlogic.com)
  * @author Remy Sharp (leftlogic.com)
  * @date 2006-12-15
  * @date 2006-12-15
-
  * @example $("img").mousehold(200, function(i){  })
+
  * @example $("img").mousehold(200, function(i){  }, 1000)
-
  * @desc Repeats firing the passed function while the mouse is clicked down
+
  * @desc Repeats firing the passed function while the mouse click is held down
  *
  *
  * @name mousehold
  * @name mousehold
Line 14: Line 19:
  * @param Number timeout The frequency to repeat the event in milliseconds
  * @param Number timeout The frequency to repeat the event in milliseconds
  * @param Function fn A function to execute
  * @param Function fn A function to execute
 +
* @param Number delay Milliseconds of delay before it should start firing
  * @cat Plugin
  * @cat Plugin
  */
  */
-
jQuery.fn.mousehold = function(timeout, f) {
+
jQuery.fn.mousehold = function(timeout, f, delay)
-
if (timeout && typeof timeout == 'function') {
+
{
-
f = timeout;
+
//Allows us to pass only one parameter
-
timeout = 100;
+
if (timeout && typeof timeout == 'function')
-
}
+
{
-
if (f && typeof f == 'function') {
+
f = timeout;
 +
timeout = 50;
 +
}
 +
 
 +
//If there is no delay, set a default
 +
if (!delay)
 +
{
 +
var delay = 250;
 +
}
 +
 
 +
//Only continue if it's been passed a function, this is infact a function
 +
if (f && typeof f == 'function')
 +
{
var timer = 0;
var timer = 0;
 +
var delaytimer = 0;
var fireStep = 0;
var fireStep = 0;
-
return this.each(function() {
 
-
jQuery(this).mousedown(function() {
 
-
fireStep = 1;
 
-
var ctr = 0;
 
-
var t = this;
 
-
timer = setInterval(function() {
 
-
ctr++;
 
-
f.call(t, ctr);
 
-
fireStep = 2;
 
-
}, timeout);
 
-
})
 
-
clearMousehold = function() {
+
return this.each(function()
-
clearInterval(timer);
+
{
-
if (fireStep == 1) f.call(this, 1);
+
jQuery(this).mousedown(function()
-
fireStep = 0;
+
{
-
}
+
fireStep = 1;
 +
var ctr = 0;
 +
var t = this;
 +
 
 +
delaytimer = setTimeout(function ()
 +
{
 +
timer = setInterval(function()
 +
{
 +
ctr++;
 +
f.call(t, ctr);
 +
fireStep = 2;
 +
}, timeout);
 +
}, delay);
 +
});
 +
 
 +
//This makes sure that as we mouse out, or mouse up - all the timers are stopped
 +
clearMousehold = function()
 +
{
 +
clearInterval(timer);
 +
clearTimeout(delaytimer);
 +
if (fireStep == 1) f.call(this, 1);
 +
fireStep = 0;
 +
}
-
jQuery(this).mouseout(clearMousehold);
+
jQuery(this).mouseout(clearMousehold);
-
jQuery(this).mouseup(clearMousehold);
+
jQuery(this).mouseup(clearMousehold);
-
})
+
});
 +
}
}
}
-
}
 

Latest revision as of 14:12, 7 June 2012

/**

*
* Modified by Andrew Spode Miller (justfdi.com, @spode) on 9th December 2009 to include a "Delay" parameter.
*
  • /

/**

* jQuery mousehold plugin - fires an event while a mouse click is held down, at a variable interval.
*
* Additionally, the function is passed a count of times the event has been fired in this session of mousehold.
*
* @author Remy Sharp (leftlogic.com)
* @date 2006-12-15
* @example $("img").mousehold(200, function(i){  }, 1000)
* @desc Repeats firing the passed function while the mouse click is held down
*
* @name mousehold
* @type jQuery
* @param Number timeout The frequency to repeat the event in milliseconds
* @param Function fn A function to execute
* @param Number delay Milliseconds of delay before it should start firing
* @cat Plugin
*/

jQuery.fn.mousehold = function(timeout, f, delay) { //Allows us to pass only one parameter if (timeout && typeof timeout == 'function') { f = timeout; timeout = 50; }

//If there is no delay, set a default if (!delay) { var delay = 250; }

//Only continue if it's been passed a function, this is infact a function if (f && typeof f == 'function') { var timer = 0; var delaytimer = 0; var fireStep = 0;

return this.each(function() { jQuery(this).mousedown(function() { fireStep = 1; var ctr = 0; var t = this;

delaytimer = setTimeout(function () { timer = setInterval(function() { ctr++; f.call(t, ctr); fireStep = 2; }, timeout); }, delay); });

//This makes sure that as we mouse out, or mouse up - all the timers are stopped clearMousehold = function() { clearInterval(timer); clearTimeout(delaytimer); if (fireStep == 1) f.call(this, 1); fireStep = 0; }

jQuery(this).mouseout(clearMousehold); jQuery(this).mouseup(clearMousehold); }); } }