I have a block that contains javascript which calls a website that sends an ad to this block (see below: Note: I changed the domain name to adserveringwebsite). How can I add code that will call the below javascript every minute so that a different ad will show up every minute instead of just one ad at page load?

<script src="http://www.adserveringwebsite.net/1e-kn99655"></script>
  • This doesn't seem to be Drupal related in any way? If you are wondering about Javascript in general, I think the question belongs on stackoverflow, and if you are wondering about the proper way to add the relevant Javascript to your site, then the question should be significantly rephrased. I fixed your <script> tags.
    – Letharion
    Jan 29 '12 at 13:20
  • Drupal 7 has an AJAX API that is not available from plain PHP/JavaScript. The OP is then asking about a block, which is a concept very specific to Drupal.
    – apaderno
    Jan 29 '12 at 16:33

To throw in some old-skool, and realize it's up to you to drupalize all of this if your ad site url is dynamic, but, you could also throw all of this in a separate static file and call it from an iframe inside your block and use refresh, eg, in the file, let's say it's /minute_by_minute_ad.htm:

    <meta http-equiv="refresh" content="60">
    <script src="http://www.adserveringwebsite.net/1e-kn99655"></script>

Then your block would contain something along the lines of:

<iframe src="/minute_by_minute_ad.htm"></iframe>

which would have a potentially added benefit of some speed improvements with ad loads in the iframe (debated heavily online but I've seen some).


You will need to use JavaScript's setTimeout() function:

To elaborate, place the code that calls your ad server inside a JS function.

Then, use setTimeout(), as demonstrated in the website I linked to, to call it at every x interval.


I think the Block Refresh module will do what you want out of the box.


Using an iframe isn't "the drupal way".

The drupal way is using AJAX to reload only the block. This is the same thing that Block Refresh does.

BUT... if you don't want the overhead of another module and you want to do it in your own module (I'm assuming your module is called 'mymodule' and the delta of the block is 'mymodule-delta'):

Make sure you include a javascript code, use this in your block content generation function:

drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js', array('every_page'=>FALSE));

The javascript file should have something like this in it:

(function ($) {
    setInterval(function() {
        $('#block-mymodule-myblock-delta .content').load('/myblock', function (response,status,xhr){
            if (status == 'error'){// some error checking
                $('#block-mymodule-myblock-delta .content').html("connection lost");
    }, 60 * 1000); // running every 60 seconds

That's going to fire an AJAX call to '/myblock' and fill the '#block-mymodule-myblock-delta' element with the content.

Now back in your module, you'll need to make the '/myblock' page like this:

function mymodule_menu() {
  $items = array();
  $items['myblock'] = array(
    'page callback' => 'myblock_via_ajax',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  return $items;

function myblock_via_ajax(){
    $block = module_invoke('mymodule','block_view','myblock-delta');
    print $block['content'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.