質問

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);
}); 
役に立ちましたか?

解決

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.

他のヒント

use setInterval to execute a function after a specified amount of time.

setInterval(someFunction(),1000);

where 1000 is the time in milliseconds which is 1 second.

the function someFunction will run every second in this case.

now using this you can adjust your logic of your code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top