Stefan Octavian Stefan Octavian - 1 year ago 56
jQuery Question

Trying to make a JQuery plugin not working

I am trying something new for me.I am always making html pages with the same functions so I want to simplify my work using a JQuery plugin made by me.But this is the First time I am trying this and I have read many web pages on how to do this and I don't know what doesn't work.This is
Emu.js file:

$.fn.loop = function(time,callback) {
if(callback !== 'undefined' && typeof(callback) == "function") {

And this is Emu.html file:

<!DOCTYPE html>
<script src="JQuery.js" ></script>
<script src="Emu.js" ></script>
<body onload="do_it()">
<script type="text/javascript">
function hello() {

function do_it() {

Answer Source
  • jQuery plugins/ Methods (just like .show()) expect a jQuery Element Object as reference: $("p").loop()
  • Put the setInterval stuff within your if
  • Use $.proxy to refer to the right jQuery this Element Object (otherwise this will point to window Object inside setInterval)
  • Inside the hello() feel free to use jQuery's $(this) cause this refers to the JS reference on the HTMLParagraphElement "p"

$.fn.loop=function(time,callback) {
  if(callback !== 'undefined' && typeof callback === "function") {
    setInterval($.proxy(callback, this), time);

jQuery(function($) { // Use DOM ready instead of <body onload="do_it()">

  function hello(){


<script src=""></script>

Some errors you've could avoid your self are i.e. the use of WindowTimers.setInterval where the first parameter is supposed to be a callback function setIntarval(fn, time). this is not a function in your case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download