Should I create a function

Should I create a function

I have a block of code that I would like to execute every 10 seconds or so and also on a buttons click event.  Should I create a function to save on coding repetation ?  I will have alot of buttons on the page that will fire this block of code, but realise I can use a class selector to trigger the code.  I need the timer interval as 3rd parties will be able to change the values that the code is checking, I am not to concerned about the timer been super accurate.
 
How would I change the code into a function?  I have seen a few examples for creating functions but I haven't seen anything that fits my requirements.
 
       //This Ajax checks the current on/off status of the passed X10 code
              $('.checkStatus').each(function(i, obj) {
               $x10Device = $(this).data("x10");



           var      data="url=http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?      t=ab&f=DeviceStatus&d="  +       $x10Device ;
              $.ajax({                  
                          url:"urlencode.php",
                          data: data,
                          type: "POST",
                        success: function(data) { 
                        myd = $('<span />').html(data).find("#Result").text();
                        var Nmyd = myd.charAt(3);
                        if (Nmyd == '2'){$('img').attr('src','lightbulbon.png')}else{$('img').attr('src','lightbulboff.png')};    
                        },
                          error: function (request, status, error) 
                     {   
                            alert(request.responseText);
                      }
             });
           });
           });