What do you need to access outside the $(function(){})? For the function you can do something like this:
var showtime; $(function(){ //... showtime = function() { //... }; }) // You can now do this showtime(); Is that what you're looking for? Karl Rudd On Thu, Feb 14, 2008 at 10:20 AM, "Sebastián V. Würtz" <[EMAIL PROTECTED]> wrote: > > thx karl, im still learning jquery is a great thing, sometimes is miss > the logic about it. > > and its work perfect, now the cpu is 5 to 11%, and i notice i must > define the function showtime inside the $(function(){} because i lose > the vars in other way, but how i can make it work from outside? i need > to redefine all my common.js script (only have 100 lines :) ) asap i > goto upload my proyect wich is a news system and have tons of jquery > scripts (the betters one). > > > Karl Rudd escribió: > > > > Part of the problem may be that the elements are being searched every > > time the function is called. > > > > $('.year').text(anio); > > $('.day').text(dia); > > $('.month').text(mes); > > $('.week').text(week); > > $('span.h').text(String(hours)); > > $('span.minute').text(String(minutes)); > > $('span.seconds').text(String(seconds)); > > $('.ampm').text(ampm); > > > > Also there is no "tag" specified for the "year", "day", "month", > > "week" and "ampm" elements. What that means is that jQuery will look > > at _every_ element on the page for one that has a "year" class, then > > every element that has a "day" class, etc. > > > > The elements can be "cached" and this _should_ improve the > > performance. This might look like: > > > > $(function() { > > showtime(); > > > > var meses = > ["ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SEP","OCT","NOV","DIC"]; > > var dias = > ["DOMINGO","LUNES","MARTES","MIERCOLES","JUEVES","VIERNES","SABADO","DOMINGO"]; > > > > var clockyear = $('.year'); > > var clockday = $('.day'); > > var clockmonth = $('.month'); > > var clockweek = $('.week'); > > var clockhour = $('span.h') > > var clockminute = $('span.minute') > > var clocksecond = $('span.seconds') > > var clockampm = $('.ampm'); > > > > function showtime() { > > > > var d = new Date(); > > var seconds = d.getSeconds(); > > var minutes = d.getMinutes(); > > var hours = d.getHours(); > > var ampm = (hours >= 12) ? " PM" : " AM"; > > var week = dias[d.getDay()]; > > var mes = meses[d.getMonth()]; > > var dia = d.getDate(); > > var anio = d.getFullYear(); > > > > hours = ((hours > 12) ? hours - 12 : hours); > > minutes = ((minutes < 10) ? "0" : "") + minutes; > > seconds = ((seconds < 10) ? "0" : "") + seconds; > > > > clockyear.text(anio); > > clockday.text(dia); > > clockmonth.text(mes); > > clockweek.text(week); > > clockhour.text(""+hours)); > > clockminute.text(""+minutes); > > clocksecond.text(""+seconds); > > clockampm.text(ampm); > > > > setTimeout(showtime, 1000); > > } > > }); > > > > Notes: > > > > A shortcut to writing String(number) can be ""+number. > > > > Instead of: > > > > setTimeout('showtime()', 1000); > > > > Changed it to : > > > > setTimeout( showtime, 1000 ); > > > > The setTimeout function can at a function or a string (which is > > "eval"s), and it's better to pass it a "real" function so it doesn't > > have to "eval" the code each time through. > > > > There are some other optimisations you could do but that's a start. > > > > Karl Rudd > > > > On Feb 14, 2008 9:23 AM, "Sebastián V. Würtz" <[EMAIL PROTECTED]> wrote: > > > >> can this code kill a cpu? > >> > >> > >> $(function() { > >> show_clock(); > >> }); > >> > >> function showtime() { > >> var meses = new > >> > Array("ENE","FEB","MAR","ABR","MAY","JUN","JUL","AGO","SEP","OCT","NOV","DIC"); > >> var dias = new > >> > Array("DOMINGO","LUNES","MARTES","MIERCOLES","JUEVES","VIERNES","SABADO","DOMINGO"); > >> > >> var d=new Date(); > >> var seconds = d.getSeconds(); > >> var minutes = d.getMinutes(); > >> var hours = d.getHours(); > >> var ampm = (hours >= 12) ? " PM" : " AM"; > >> var week = dias[d.getDay()]; > >> var mes = meses[d.getMonth()]; > >> var dia = d.getDate(); > >> var anio = d.getFullYear(); > >> > >> hours = ((hours > 12) ? hours - 12 : hours); > >> minutes = ((minutes < 10) ? "0" : "") + minutes; > >> seconds = ((seconds < 10) ? "0" : "") + seconds; > >> > >> $('.year').text(anio); > >> $('.day').text(dia); > >> $('.month').text(mes); > >> $('.week').text(week); > >> $('span.h').text(String(hours)); > >> $('span.minute').text(String(minutes)); > >> $('span.seconds').text(String(seconds)); > >> $('.ampm').text(ampm); > >> > >> setTimeout('showtime()', 1000); > >> } > >> > >> the problem is the setTimeout('showtime()', 1000); > >> if i uncomment the show_clock() the cpu usage is normal, but is not, the > >> cpu usage is at 50%+ in ie6. > >> > >> > >> > >> >