Search code examples
jqueryajaxpage-refreshemoticons

Refreshing the page after executing jQuery function on content


I have a jQuery function which applies some formatting to the entire HTML page. I have this function execute successfully.

jQuery.fn.emoticons= function(iconfolder) {
    //some operations
};

This function purpose is to transform all smilies(:D) to a smiley image. The function has effect on the entire HTML content.

What I am trying to achieve is that, I want this function to be executed every x second and based on it the page content should be changed.

With my limited knowledge on jQuery, I understand that $.ajax() can be used based on a URL and not on a function.

What's the best way to achieve this?

EDIT:

Based on below inputs, I tried this and works. Thanks all.

$(function() {
   setInterval(function() { $(".ow_content").emoticons() },5000);
}); 

Solution

  • Just set up an interval with setInterval():

    $.fn.emotions = function() {
       currentHTML = $("body").html();
       newHTML = currentHTML.replace(/:D/,"<img src='/your/image/path'/>");
       $("body").html(newHTML);
    };
    
    $(function() {
        waitSeconds = 5;
        setInterval($.fn.emotions, waitSeconds*1000);
    });
    

    And here's the jsFiddle to let you play with it.