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

  • SEARCH
  • Home
  • 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 6622249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:22:54+00:00 2026-05-25T21:22:54+00:00

I’m building my first jquery plugin and would like some tips to do it

  • 0

I’m building my first jquery plugin and would like some tips to do it better. I also have an error in my code which I like to get rid off. I’m trying to refresh the content of my widget via a setTimeout in my ajax success callback. it is working (without argument) but I like to pass an argument like so

setTimeout(refresh(o.refresh), 5000);

I’m not sure it is done like that but I followed my intuition. I have the following error in firebug

useless setTimeout call (missing quotes around argument?)

I don’t understand this error because the argument provided to the refresh function is a variable. I need to pass an argument to see if the event is triggered by a user double click (widget toggles open) or by the setTimeout method (widget is open so there is no need to close it). I’m not sure that what I’m trying to do is even possible. I could solve my problem by adding a conditional ajax call for the refresh option but I don’t want duplicate code. hope anyone can give me some hints ‘n’ tips, not only for my error but also in general (plugin development). as a jquery starter I’m not sure my code is conventional.

peace

        /**
 * @author kasperfish
 */
(function($){
    $.fn.extend({

    widgetIt: function(options) {

        var defaults = {
            title: 'Widget Title',
            load:'',
            top: '50px',
            left: '400px',
            width: '500px',
            afterLoad: function(){},
            reload:false,
            refresh:true
        };

        var options = $.extend(defaults, options);
        var o=options;

        return this.each(function() { 

              var container=$(this).css({'z-index':3, display:'inline-block',position:'absolute',top:o.top,left:o.left,'max-width':o.width})
                            .addClass('widget_container');
              var header = $('<div></div>')
                            .addClass('ui-widget-header widgethead')
                            .css({'min-width':'130px'});

              var title =$('<div></div>').addClass("w_tit").html(o.title);
              var content =$('<div></div>')
                           .addClass("w_content")
                           .hide();


              //append
              $(title).appendTo(header) ;
              $(header).appendTo(container) ;
              $(content).appendTo(container) ;

              //make draggable
              $(container).draggable({
                cancel: 'input,option, select,textarea,.w_content',
                opacity: 0.45,
                cursor: 'move'
                });

              //binding
                var display=$(content).css('display'); //check if widget is open=block or closed=none
                var reload=true ; //set initially to true->reload content every time widget opened
                var refreshcontent=false;
              $(header).dblclick(function refresh(refreshcontent){

                if(!refreshcontent)//if it's not a refresh
                $(content).fadeToggle();//open or close widget
                //[show ajax spinner]

                    if(display="block" && reload){//only load on widget open event

                      $.ajax({
                        url: o.load,
                        context: content,
                        success: function(data){
                        $(content).html(data);
                        reload=false;
                        //[hide ajax spinner]
                        setTimeout(refresh(o.refresh), 5000);//refresh every 5s
                        o.afterLoad.call();},
                        error: function(){
                        // error code here
                        }
                        }); 
                    }else if(display="none"){reload=o.reload;}//set reload true or false
              });


             $(header).click(function (){
                $(container).topZIndex('.widget_container');

             });
             //close all open widgets and animate back to original position
             $('#deco').click(function (){
                        $(content).hide();
                        $(container).animate({ "left": o.left, "top": o.top}, "slow");

             });  
        });
    }
});
})(jQuery);
  • 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-25T21:22:55+00:00Added an answer on May 25, 2026 at 9:22 pm

    You’re passing the result of refresh(o.refresh) to setTimeout, which is why it’s useless. Instead, you need to pass a function that calls refresh(o.refresh) when the setTimeout fires:

    setTimeout(function () { refresh(o.refresh); }, 5000);
    

    As for “tips” to make your code better, I suggest you ask that part of your question on Code Review, which is more suited to that type of question.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.