ElementSpy

ElementSpy allows you to assign periodical checks on element attributes or other aspects of elements.

Download @ Forge JS

 

MooTools Javascript Class

/*
---
description:     ElementSpy

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - ElementSpy
...
*/
var ElementSpy = new Class({
	Implements: [Options,Events],
	options: {
		interval: 100
	},
	initialize: function(element,attribute,options) {
		this.setOptions(options);
		this.element = document.id(element);
		this.val = this.getter(attribute);
		this.interval;
		this.worker = function() {
			var value = this.getter(attribute);
			this.fireEvent('check',[this.val,value]);
			if(value != this.val) {
				this.fireEvent('change',[this.val,value]); //old,new
				this.val = value;
			}
			else {
				this.fireEvent('stagnate',[value]); //current
			}
		}.bind(this);
	},
	start: function() {
		this.interval = this.worker.periodical(this.options.interval);
		this.fireEvent('start');
		return this;
	},
	stop: function() {
		$clear(this.interval);
		this.fireEvent('stop');
		return this;
	},
	getter: function(attribute) {
		return $type(attribute) == 'function' ? (attribute.bind(this.element))() : this.element.get(attribute);
	}
});

Class: ElementSpy

ElementSpy allows you to assign periodical checks on element attributes or other aspects of elements.

Implements:

Options, Events

ElementSpy Method: constructor

Syntax:

var myElementSpy = new ElementSpy(element,attribute,options);

Arguments:

  1. observeElement - (string or Element) The element to spy on.
  2. attribute - (string or Function) A string representing the element attribute to check or a function which returns the value to compare each cycle.
  3. options - (object) An object containing the ElementSpy instance's options.

Options:

  • interval - (number, defaults to 100) The interval at which the Element should be checked.

Returns:

A ElementSpy instance.

Events:

start

  • (function) Function to execute to start the listener.

Signature

onStart()

stop

  • (function) Function to execute to star the listener.

Signature

onStop()