{"id":626,"date":"2010-06-18T09:42:26","date_gmt":"2010-06-18T08:42:26","guid":{"rendered":"http:\/\/www.jaconet.org\/?p=626"},"modified":"2025-02-24T22:56:52","modified_gmt":"2025-02-24T21:56:52","slug":"ajaxobject-tips-periodic-update-of-contents","status":"publish","type":"post","link":"https:\/\/www.lemie5lire.com\/?p=626","title":{"rendered":"ajaxObject tips: periodic update of contents"},"content":{"rendered":"<p style=\"text-align: justify;\">One of the possibles use of ajax technology is to have an automatic update of data in a web page.This is easily possible using the ajaxObject.<\/p>\n<p style=\"text-align: justify;\">First of all, we have to think to an object that is available in the ajaxObject: timerObject.<\/p>\n<p style=\"text-align: justify;\">This is normally used inside ajaxObject for timeout and progress bar, but it can also be used any time a timer is needed.<\/p>\n<p style=\"text-align: justify;\">So, what code to do this?<\/p>\n<p style=\"text-align: justify;\">Firstly let define a new ajaxObject to prepare a request for content.<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"javascript codecolorer\"><span class=\"sy0\">&lt;<\/span>p style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text-align: justify;&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>span style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>em<span class=\"sy0\">&gt;<\/span><span class=\"kw1\">var<\/span> af1<span class=\"sy0\">=<\/span><span class=\"kw1\">new<\/span> ajaxObject<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;ajax.php&quot;<\/span><span class=\"sy0\">,<\/span><span class=\"st0\">&quot;ID1&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;&lt;\/<\/span>em<span class=\"sy0\">&gt;&lt;\/<\/span>span<span class=\"sy0\">&gt;&lt;\/<\/span>p<span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p style=\"text-align: justify;\">This has been already discussed in my previous a<a href=\"..\/..\/..\/..\/..\/?p=601\">rticle.<\/a><\/p>\n<p style=\"text-align: justify;\">Then we have to create a new timer that will make the request:<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"javascript codecolorer\"><span class=\"sy0\">&lt;<\/span>p style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text-align: justify;&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>em<span class=\"sy0\">&gt;&lt;<\/span>span style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span><span class=\"sy0\">&gt;<\/span><span class=\"kw1\">var<\/span> ti1<span class=\"sy0\">=<\/span><span class=\"kw1\">new<\/span> timerObject<span class=\"br0\">&#40;<\/span><span class=\"nu0\">5000<\/span><span class=\"sy0\">,<\/span><span class=\"kw1\">function<\/span> <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>af1.<span class=\"me1\">update<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;t=3&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;&lt;\/<\/span>span<span class=\"sy0\">&gt;&lt;\/<\/span>em<span class=\"sy0\">&gt;&lt;\/<\/span>p<span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p style=\"text-align: justify;\">The three parameters of timeObject call are:<\/p>\n<p style=\"text-align: justify;\">1. Timing in milliseconds.<\/p>\n<p style=\"text-align: justify;\">2. Function to be executed, in this case is an ajax request<\/p>\n<p style=\"text-align: justify;\">The third optional parameter would be the ID of &lt;span&gt; or &lt;div&gt; where to show a progress bar, but this is not needed since the progress bar will be displayed by the ajaxObject.<\/p>\n<p style=\"text-align: justify;\">Finally we have to activate the timing. This can be done calling:<\/p>\n<div class=\"codecolorer-container javascript default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"javascript codecolorer\"><span class=\"sy0\">&lt;<\/span>p style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text-align: justify;&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>span style<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>em<span class=\"sy0\">&gt;<\/span>ti1.<span class=\"me1\">start<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;&lt;\/<\/span>em<span class=\"sy0\">&gt;&lt;\/<\/span>span<span class=\"sy0\">&gt;&lt;\/<\/span>p<span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p style=\"text-align: justify;\">that can be placed were you need. For example using onload:<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"html4strict codecolorer\"><span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/p.html\"><span class=\"kw2\">p<\/span><\/a> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text-align: justify;&quot;<\/span>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;<\/span><span class=\"sc1\">&amp;lt;<\/span>body onload='ti1.start();'<span class=\"sc1\">&amp;gt;<\/span><span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/p.html\"><span class=\"kw2\">p<\/span><\/a>&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p style=\"text-align: justify;\">This will do the job. Of course, the first call will be done after the timeout expires (in our example 5 seconds) and you might dislike a similar behavior and you might want the first call to be done just after loading the page. If so, just change the body call in the following way:<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"html4strict codecolorer\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/p.html\"><span class=\"kw2\">p<\/span><\/a> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text-align: justify;&quot;<\/span>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;&amp;lt;body <span class=\"kw3\">onload<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">'af1.update(&quot;t=3&quot;);af1.showprogressbar=false;ti1.start();'<\/span>&amp;gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/p.html\"><span class=\"kw2\">p<\/span><\/a>&gt;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p style=\"text-align: justify;\">The code <span style=\"color: #333399;\"><em>af1.showprogressbar=false;<\/em><\/span> just disable the progress bar, so old content remains until a new one is available.<\/p>\n<p style=\"text-align: justify;\">That&#8217;s all folks!<\/p>\n<p>Here is the full HTML code:<\/p>\n<div class=\"codecolorer-container html4strict default\" style=\"overflow:auto;white-space:nowrap;width:90%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/><\/div><\/td><td><div class=\"html4strict codecolorer\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a> <span class=\"kw3\">style<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;color: #333399;&quot;<\/span>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;&amp;lt;html&amp;gt;<br \/>\n&amp;lt;head&amp;gt;<br \/>\n&amp;lt;script <span class=\"kw3\">src<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;ajax.js&quot;<\/span>\u00a0 <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text\/javascript&quot;<\/span>&amp;gt;&amp;lt;<span class=\"sy0\">\/<\/span>script&amp;gt;<br \/>\n&amp;lt;script <span class=\"kw3\">type<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;text\/javascript&quot;<\/span> &amp;gt;<br \/>\nvar af1<span class=\"sy0\">=<\/span>new ajaxObject<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;ajax.php&quot;<\/span>,<span class=\"st0\">&quot;ID1&quot;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\nvar ti1<span class=\"sy0\">=<\/span>new timerObject<span class=\"br0\">&#40;<\/span><span class=\"nu0\">5000<\/span>,function <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>af1.update<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;t=3&quot;<\/span><span class=\"br0\">&#41;<\/span>;<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n&amp;lt;<span class=\"sy0\">\/<\/span>script&amp;gt;<br \/>\n&amp;lt;<span class=\"sy0\">\/<\/span>head&amp;gt;<br \/>\n&amp;lt;body <span class=\"kw3\">onload<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">'af1.update(&quot;t=3&quot;);&lt;\/em&gt;&lt;\/span&gt;&lt;span style=&quot;color: #333399;&quot;&gt;&lt;em&gt;af1.showprogressbar=false;&lt;\/em&gt;&lt;\/span&gt;&lt;span style=&quot;color: #333399;&quot;&gt;&lt;em&gt;ti1.start();'<\/span>&amp;gt;<br \/>\n&amp;lt;div&amp;gt;&amp;lt;span&amp;gt;Your dynamic <span class=\"kw3\">content<\/span> is: &amp;lt;<span class=\"sy0\">\/<\/span>span&amp;gt;&amp;lt;<span class=\"kw3\">span<\/span> <span class=\"kw3\">id<\/span><span class=\"sy0\">=<\/span><span class=\"st0\">&quot;ID1&quot;<\/span>&amp;gt;First&amp;lt;<span class=\"sy0\">\/<\/span>span&amp;gt;&amp;lt;<span class=\"sy0\">\/<\/span>div&amp;gt;<br \/>\n&amp;lt;<span class=\"sy0\">\/<\/span>body&amp;gt;&amp;lt;<span class=\"sy0\">\/<\/span>html&amp;gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/em.html\"><span class=\"kw2\">em<\/span><\/a>&gt;&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/span.html\"><span class=\"kw2\">span<\/span><\/a>&gt;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<div class=\"pdfprnt-buttons pdfprnt-buttons-post pdfprnt-bottom-right\"><a href=\"https:\/\/www.lemie5lire.com\/index.php?rest_route=wpv2posts626&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><\/a><a href=\"https:\/\/www.lemie5lire.com\/index.php?rest_route=wpv2posts626&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/www.lemie5lire.com\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Stampa contenuto\" \/><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>One of the possibles use of ajax technology is to have an automatic update of data in a web page.This is easily possible using the ajaxObject. First of all, we have to think to an object that is available in the ajaxObject: timerObject. This is normally used inside ajaxObject for timeout and progress bar, but &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ghostkit_customizer_options":"","ghostkit_custom_css":"","ghostkit_custom_js_head":"","ghostkit_custom_js_foot":"","ghostkit_typography":"","iawp_total_views":0,"footnotes":""},"categories":[1,13],"tags":[87,88,86],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-generale","category-technologie","tag-ajax","tag-ajaxobject","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=626"}],"version-history":[{"count":9,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/posts\/626\/revisions"}],"predecessor-version":[{"id":47481,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=\/wp\/v2\/posts\/626\/revisions\/47481"}],"wp:attachment":[{"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lemie5lire.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}