Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:51:31+00:00 2026-05-15T19:51:31+00:00

I wahnt to write a jQuery plugin which will change the x background-position in

  • 0

I wahnt to write a jQuery plugin which will change the x background-position in an looped time interval.
$('#element').pluginName(); – starts the looped action

Then I wahnt to stop it by $('#element').stopFunction(); or $('#element').pluginName().stopFunction();

Is it possible? Can You give me a tip how to write it?

EDIT (my solution):

var LoadingAjax = function(element, options){
    var element = $(element);
    var object = this;
    var defaults = {
            width: 30,
            gap : 1,
            interval : 100,
            maxFrame: 8
    };
    var settings = $.extend(defaults, options || {});
    var timer;
    var frame = 1;
    var is_loading;

    // Public method
    this.startAnimate = function(){
        console.log('start animate');
        is_loading = true;
        timer = setTimeout(loop, settings.interval);
    };
    this.stopAnimate = function(){
        console.log('stop animate');
        is_loading = false;
        clearTimeout(timer);
    };
    this.isLoading = function(){
        return is_loading;
    }
    // Private method
    var loop = function(){
        console.log('frame: '+frame);
        if(frame < defaults.maxFrame){
            element.css('background-position', '-'+(frame*(defaults.width+defaults.gap))+'px 0');
            frame++;
        }
        else{
            element.css('background-position', '0 0');
            frame = 1;
        }
        timer = setTimeout(loop, settings.interval);
    };
};
$.fn.loadingAjax = function(options){
    return this.each(function(){
        var element = $(this);
        // Return early if this element already has a plugin instance
        if (element.data('loader')) return;

        // pass options to plugin constructor
        var plugin_instance = new LoadingAjax(this, {});

        // Store plugin object in this element's data
        element.data('loader', plugin_instance);
    });
};

$(document).ready(function(){
    $('a#start').click(function(){
        var loadingElement = $('.loading.ajax');
        loadingElement.loadingAjax();
        var plugin_instance = loadingElement.data('loader');
        if(plugin_instance.isLoading() === true){
            plugin_instance.stopAnimate();
        }
        else{
            plugin_instance.startAnimate();
        }
    });
});

This link was very helpful: http://www.virgentech.com/blog/2009/10/building-object-oriented-jquery-plugin.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:51:32+00:00Added an answer on May 15, 2026 at 7:51 pm
    $.fn.pluginName = function(){
        return this.each( function(){
            //Something cool and fancy happens here.
    
            //Remember you can have access to the element using the variable 'this'.
        }
    }
    

    will make your plugin available for calling $('#element').pluginName()

    Now, about the intervals, you can make like this:

    var holder = setInterval( function(){
        //The code here repeats every half a second.
    }, 500 );
    

    And then clean it with this:

    clearInterval( holder );
    

    The only problem is you have to be very cautious with the scope. Making holder a global variable should do the trick, but I recommend you take the chance for learning some sexy jutsus by reading this before.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to write a plugin which will pass a variable stored as input(which
I want write a simple query which will fetch data from a table (which
I'm working on an app that uses a jQuery UI plugin, which in turn
I'm attempting to write my first jQuery plugin to query multi-dimensional arrays of complex
I'm trying to write a new jQuery plugin. base (this is not my plugin,
I want to write a jQuery plugin that does: 2012-12-31 23:00:00 -> the following
I wrote a beginner jQuery plugin which validate a form. this jQuery Plugin show
i'm trying to write a jquery plugin that when i click on a image
I'm trying to write my own jQuery plugin. My goal is to attach it
For a JQuery plugin that I am trying to write I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.