Topic: Simple Countdown, need help.

Hi, I am simply trying to countdown 16 seconds from when the page loads, and then at the end of that 16 seconds make a rails call.

This is how I am doing it now:

    <!--
    var g_iCount = new Number();

    // CHANGE THE COUNTDOWN NUMBER HERE //
    var g_iCount = 17;

    function startCountdown(){
           if((g_iCount - 1) >= 0){
                   g_iCount = g_iCount - 1;
                   $('seconds').update(g_iCount);
                   setTimeout('startCountdown()',1000);
           }
                 else {
                    $('credits').update(<%= current_user.total_points + 1 %>);
                    <% current_user.point.add_point %>
                }
    }
    //  End -->


and in the body tag on the page:
<body onLoad="startCountdown()">

The problem is, the execution of current_user.point.add_point happens immediately.  I cannot figure that out!

Last edited by dishkuvek (2007-08-14 17:22:30)

Re: Simple Countdown, need help.

You can just call your javascript function in the setTimeout - you are replicating what it does.

The ruby code will be executed on the server. Check the source of your page is the output of current_user.point.add_point a javascript snippet? (It probably shouldn't be - models shouldn't get mixed up with your views)

If not, it will get executed when the template is processed on the server.

You need something like:

setTimeout("callUpdate",16000);

function callUpdate() {
  new Ajax.Request("url_to_controller_action",{options});
}

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Simple Countdown, need help.

Excellent!  Thank you, I've got it working now with this:

    var g_iCount = 17;

    function startCountdown()
    {
      if((g_iCount - 1) >= 0)
        {
            g_iCount = g_iCount - 1;
            $('seconds').update(g_iCount);
            setTimeout('startCountdown()',1000);
        }
        else
        {
            new Ajax.Request('/point/add_user_point', {asynchronous:true});
        }        
    }


add_user_point is a method in the point controller which just adds a point and calls an RJS to update an element on the page.

Thank you, you suggestion was very helpful.