Converting Javascript function to Drupal

bsawyer - August 7, 2008 - 16:29

I am having issues converting a count down timer from my old site to Drupal 6. Here is my code. The non-javascript portions work fine but I can't get the text box to populate and count down.

<?PHP

drupal_add_js('
var up,down;
var min1,sec1;
var cmin1,csec1,cmin2,csec2;
function Minutes(data) {
for(var i=0;i";

$now = date("d M y H:i:s");

$remaining = 10000;
$min=floor($remaining/60);
$sec=$remaining-$min*60;

print "";
print "$now Time Remaining: ";
print "$min minutes : $sec seconds";

drupal_add_js('Down();', 'inline');

?>

maybe checking out the code

WorldFallz - August 7, 2008 - 16:36

maybe checking out the code for http://drupal.org/project/countdown will help...

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

I tried

bsawyer - August 7, 2008 - 16:55

I tried using that code and was unsuccessful as well. It seemed like a lot of folks have issues getting that module to work so I thought I would stay with what worked for me in the past and I somewhat understand. I suspect there is something simple and obvious I am missing but my javascript knowledge is limited.

one thing i just noticed--

WorldFallz - August 7, 2008 - 17:26

one thing i just noticed-- you're missing the closing ")" from the first drupal_add_js. typo? Also, you can't just include random $php_variables in your js. You have to concatenate them in like this:

<?php
$init
= 1;
drupal_add_js('
var var1;
var var2;
for(var i='
. $init . '; i <= 10; i++)
{
  alert("hello");
}
'
);
?>

And be careful of quotes-- if you're delimiting the js with single quotes for the drupal_add_js function, you can only use double-quotes within the js code itself or the function won't be properly delimited.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

Apparently when I pasted in

bsawyer - August 7, 2008 - 18:49

Apparently when I pasted in the code most of it was dropped from the post.

I did try pasting in your example script and that didn't work either. It seems my issue is more basic.

I have input_format set to php code. And Optimize JavaScript files: disabled. Is there somewhere I need to enable javascript for drupal?

Thanks for your help.

meh... my bad... try

WorldFallz - August 7, 2008 - 19:35

meh... my bad... try this:

<?php
$init
= 1;
drupal_add_js('
for(var i='
. $init . '; i <= 3; i++)
{
  alert("hello");
}
'
, 'inline');
?>

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

That worked. I used that

bsawyer - August 7, 2008 - 22:13

That worked. I used that same approach and got my count down timer working as well except it only works when I use the alert and then only when the page loads before the alert pops up such as in the "preview" mode. Below is the actual code.

<?PHP

drupal_add_js('
      var up,down;
      var min1,sec1;
      var cmin1,csec1,cmin2,csec2;
      function Minutes(data) {
         for(var i=0;i<data.length;i++) if(data.substring(i,i+1)==":") break;
         return(data.substring(0,i)); }
      function Seconds(data) {
         for(var i=0;i<data.length;i++) if(data.substring(i,i+1)==":") break;
         return(data.substring(i+1,data.length)); }
      function Display(min,sec) {
         var disp;
         if(min<=9) disp=" 0";
         else disp=" ";
         disp+=min+":";
         if(sec<=9) disp+="0"+sec;
         else disp+=sec;
         return(disp); }
      function Down() {
         alert("hello");
         cmin2=1*Minutes(document.sw.beg2.value);
         csec2=0+Seconds(document.sw.beg2.value);
         DownRepeat(); }
      function DownRepeat() {
         csec2--;
         if(csec2==-1) { csec2=59; cmin2--; }
         document.sw.disp2.value=Display(cmin2,csec2);
         if((cmin2==0)&&(csec2==0)) document.sw.disp2.value="- - : - -";
         else down=setTimeout("DownRepeat()",1000); }

',
'inline'
);

print "<FORM name=sw method=POST action=$PHP_SELF>";

   $now = date("d M y H:i:s");
  
   $remaining = 40;
   $min=floor($remaining/60);
   $sec=$remaining-$min*60;

   print "<input type=hidden name=beg2 size=7 value=\"$min:$sec\">";
   print "$now  Time Remaining: <input type=text name=disp2 size=6>";
   print "$min minutes : $sec seconds";

   drupal_add_js('Down();', 'inline');
 
  ?>

i tried the code also and

WorldFallz - August 7, 2008 - 23:44

i tried the code also and got the same results as you. not really sure what could be the problem-- hopefully a js guru will speak up....

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

You haven't got any onload()

dman - August 8, 2008 - 00:11

You haven't got any onload() deferral going on there.
So the updates attempt to start running before the page is fully loaded. Before the input element exists and is writable.
This most likely causes an error and stops any further scripts from running.

You should install a javascript debugger ... or at least enable script error alerts in your browser. That would have told you what was going wrong. In both FF and IE there is a small alert icon that shows up in the corner of the browser when it encounters bad scripts

There's a bunch of other issues with that code also, but to take some steps forward, you may want to try the defer=try parameter to the javascript embedding.
Much better is to start learning jquery and look at some examples from existing drupal utils to see how they wait for document.ready.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

 
 

Drupal is a registered trademark of Dries Buytaert.