Oh wow ... no offense but I haven't seen such code in a while :) .. I
see the code but it's difficult to understand the problem that you are
trying to solve ... looks like you are building a clock. For building
reasonable small JavaScript I'd recommend using Lif't's JavaScript
abstraction (JsExp/JsCmd etc). For bigger JS code I'd recommend
writing the JavaScript code in the JS file and define your own
JavaScript objects.

So what I'd do:

1. Define the JavaScript function in a separate .js file (the function
may be part of a JS JSON object )
2. The function would take a JSON structure as argument
3. From the Lift's snippet generate the JSON structure that needs to
be passed to you function using JsObj and generate the <script> tag
(using xml literals or even use the JsCmds.Script object). Potentially
use JqOnLoad so that
    your function to be called after the DOM tree is loaded



Br's,
Marius

On Apr 26, 1:31 pm, "sailormoo...@gmail.com" <sailormoo...@gmail.com>
wrote:
> Here is the current solution. It's not clean, but I cannot think of
> better idea for the code.
>
>  bind("entry", xhtml,
>       "js_realtimeoutput" -> <script language="JavaScript">{Unparsed
> (js_realtime_output(room_day, room))}</script>)
>
> private def js_realtime_output(room_day: RoomDay, room: Room) : String
> = {
>     def date_string(datetime : GregorianCalendar) : String  = {
>       String.format("new Date(%s,%s,%s,%s,%s,%s)",
>         datetime.get(Calendar.YEAR).toString, datetime.get
> (Calendar.MONTH).toString,
>         datetime.get(Calendar.DAY_OF_MONTH).toString, datetime.get
> (Calendar.HOUR_OF_DAY).toString,
>         datetime.get(Calendar.MINUTE).toString, datetime.get
> (Calendar.SECOND).toString)
>     }
>
>     val s_datetime = new GregorianCalendar()
>     val e_datetime = new GregorianCalendar()
>     if (room_day.day_no.is == 0) {
>        """function realtime_output() {
>          php_now = """ + date_string(s_datetime) + """;
>          local_now = new Date();
>          diff_sec = Math.floor( (local_now - php_now) / 1000);
>          document.realtime_form.realtime_output.value = "伺服器與本地時間差:" +
> diff_sec + "秒";
>        }"""
>     } else {
>       s_datetime.setTime(room_day.created.is)
>       e_datetime.setTime(room_day.created.is)
>
>       var until_string = ""
>       if (room_day.day_no.is % 2 == 0) {
>         e_datetime.add(Calendar.MINUTE, room.day_minutes.is)
>         until_string = "日落"
>       } else {
>         e_datetime.add(Calendar.MINUTE, room.night_minutes.is)
>         until_string = "早上"
>       }
>
>       """starttime = """ + date_string(s_datetime) + """;
>       endtime   = """ + date_string(e_datetime) + """;
>       diffseconds = Math.floor((endtime - starttime)/1000);
>       function realtime_output() {
>         nowtime = new Date();
>         leftseconds = diffseconds - Math.floor((nowtime - starttime)/
> 1000);
>         lefttime = new Date(0,0,0,0,0,leftseconds);
>         virtual_left_seconds = Math.floor(12*60*60*(leftseconds /
> diffseconds));
>         virtual_lefttime = new Date(0,0,0,0,0,virtual_left_seconds);
>         if(leftseconds > 0){
>           document.realtime_form.realtime_output.value = " """ +
> until_string + """剩餘 " + virtual_lefttime.getHours()+"時
> 間"+virtual_lefttime.getMinutes()+"分 (實際時間 "+lefttime.getMinutes()
> +"分"+lefttime.getSeconds()+"秒)";
>         } else {
>           overseconds = Math.abs(leftseconds);
>           overtime = new Date(0,0,0,0,0,overseconds);
>           document.realtime_form.realtime_output.value = "超過時間
> "+overtime.getMinutes()+"分"+overtime.getSeconds()+"秒";
>         }
>         tid = setTimeout('realtime_output()', 1000);
>       }"""
>     }
>   }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to