Can't get uptime script to work on system status gadget.
-
Tuesday, September 11, 2012 7:17 PM
I've been trying to fix a gadget of mine that I use a lot. The system uptime component never works on any of my computers, and I assume it's because the original script used a visual basic API that is not 64 bit. I have copied a javascript uptime script into it and I've tried to get it to work, but it doesn't and i cant figure out why. any help would be appreciated.
http://www.2shared.com/file/0YKlGLGx/ComputerStatus_v4gadget.html
All Replies
-
Wednesday, September 12, 2012 11:33 AM
Have no time right now to look at yours so here is a complete gadget solely for the purpose of displaying uptime.
It uses wmi methods in a vbscript to find the uptime when the gadget is first started and stores this value in a global then simply gets the current time every second, subtracts the value of the global and displays the uptime.
Been a while since I've had a look at this so my script may be a bit dodgy - feel free to poke fun at it.
Hope it works for you.<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>UpTimer</title> <style type="text/css"> <!-- body {margin:0;width:130px;height:60px;padding:5px;font:12px/14px Arial;background-color:#0c0;text-align:center;} #sysUpTime {margin:0;width:110px;height:30px;padding:10px;color:#000;background-color:#888;font-weight:bold;} --> </style> <script type="text/vbscript"> Function GetSystem() On Error Resume Next Dim objWMI, objLocator, colItems, strQuery, objItem, strSysDrive, intUpTime Set objLocator = CreateObject( "WbemScripting.SWbemLocator" ) Set objWMI = objLocator.ConnectServer( ".", "root\cimv2" ) strQuery = "SELECT * FROM Win32_PerfFormattedData_PerfOS_System" Set colItems = objWMI.ExecQuery( strQuery, "WQL", 32 ) For Each objItem In colItems intUpTime = objItem.SystemUpTime Next GetSystem = intUpTime Set colItems = Nothing Set objWMI = Nothing Set objLocator = Nothing End Function </script> <script type="text/jscript"> var t=null; var upZero=0; Number.prototype.aZ=function(){var num=(this>9)?this:"0"+this;return num;}; function getUptime(A){ var d=new Date(); upZero=(Math.round(d.getTime()/1000))-A; } function showUptime(){ var secs, mins, hrs, dys, tdisplay, upTime; tdisplay=new Date(); upTime=(Math.round(tdisplay.getTime()/1000))-upZero; secs=(upTime%60).aZ(); mins=((Math.floor(upTime/60))%60).aZ(); hrs=((Math.floor(upTime/3600))%24).aZ(); dys=(upTime<86400)?"":(Math.floor(upTime/86400))+":"; tdisplay=dys+hrs+":"+mins+":"+secs; return tdisplay; } function x(){ sysUpTime.innerText=showUptime(); t=setTimeout(x,1000); } function fx(){ var getsys=GetSystem(); getUptime(getsys); x(); } </script> </head> <body onload="fx()"> <div>System Uptime</div> <div id="sysUpTime"> </div> </body> </html>
- Edited by mystifeid Wednesday, September 12, 2012 11:38 AM
-
Friday, September 14, 2012 10:36 AM
Back home at last.
There are two scripts in your gadget with uptime functions in them : SystemUptimeFull_Plus.js and uptime.js. Since the supplied html calls uptime.js it is the one I've looked at.
There are a few things stopping it from working.function uptime(){ document.getElementById('uptimeTxt').innerTxt = "test"; //the property is innerText NOT innerTxt //also since this is a jscript environment it is not necessary to use document.getElementById //so that last line should be just // uptimeTxt.innerText="test"; var now = new Date(); var seconds = (now - lastBootUpTime)/1000; var days = Math.floor(seconds/60/60/24); seconds -= (days*24*60*60); var hours = Math.floor(seconds/60/60); seconds -= (hours*60*60); var minutes = Math.floor(seconds/60); seconds = seconds - (minutes*60); seconds = Math.floor(seconds); if (days>=maxday) maxday *= 2; imgbarday.style.width = parseInt(days * maxlengthallbars * size / maxday); imgbarhours.style.width = parseInt(hours * maxlengthallbars * size / maxhrs); imgbarminutes.style.width = parseInt(minutes * maxlengthallbars * size / maxmin); imgbarseconds.style.width = parseInt(seconds * maxlengthallbars * size / maxsec); //haven't looked that hard but can't seem to find any elements with the id's imgbarday, imgbarhours etc so comment the last four lines // besides parseInt looks to be unnecessary (the value of size would be coerced to type integer) and .style.width should be coerced back to a string // eg // imgbarday.style.width = (days * maxlengthallbars * size / maxday)+"px"; // // but if imgbarday was the id of an image then the value of imgbarday.width (without the .style) could be an integer //eg //imgbarday.width = days * maxlengthallbars * size / maxday; var uptime; // you have just created a local variable with the same name as the function - this would prevent a setTimeout that was using a function pointer from working. // you must either change the name of the function or change the name of the variable. // when choosing names please try to make them unique. uptime=""; uptime += days; if (days == 0) { uptime += " day, ";} //this should be // if (days == 1) { uptime += " day, ";} // this will give 0 days and 1 day rather than 0 day and 1 days else {uptime += " days, ";} if (hours < 10) uptime += "0"; uptime += hours + ":"; if (minutes < 10) uptime += "0"; uptime += minutes + ":"; if (seconds < 10) uptime += "0"; uptime += seconds; document.getElementById('uptimeTxt').innerTxt = uptime; //again wrong property ( innerTxt ) and unnecessary use of document.getElementById // last line should be //uptimeTxt.innerText=uptime; var secondOffset = secondTimeOffset(); if (secondOffset > 0) uptimeTimeOut = setTimeout("Uptime()", secondOffset); else Uptime(); // the value returned by the getMilliseconds method will always be in the range from 0 to 999 // so secondOffset will ALWAYS be greater than 0. // also you are using setTimeout with a function name that does not exist - Uptime() - it should instead be using uptime() (uncapitalized) // and you are using eval in the setTimeout to reference a function from a string - use a function pointer instead - no brackets and no inverted commas // so the previous two lines should be just ( after you change either the function name or the local variable name ) // uptimeTimeOut = setTimeout(uptime, secondOffset); } function secondTimeOffset(){ return 1000 - new Date().getMilliseconds(); //this value will always be greater than zero }so changing your script as little as possible to get it working gives
function uptime(){ uptimeTxt.innerText = "test"; //creates new date object based on now var now = new Date(); var seconds = (now - lastBootUpTime)/1000; var days = Math.floor(seconds/60/60/24); seconds -= (days*24*60*60); var hours = Math.floor(seconds/60/60); seconds -= (hours*60*60); var minutes = Math.floor(seconds/60); seconds = seconds - (minutes*60); seconds = Math.floor(seconds); //if (days>=maxday) maxday *= 2; //settings for img bar //imgbarday.style.width = (days * maxlengthallbars * size / maxday)+"px"; //imgbarhours.style.width = parseInt(hours * maxlengthallbars * size / maxhrs); //imgbarminutes.style.width = parseInt(minutes * maxlengthallbars * size / maxmin); //imgbarseconds.style.width = parseInt(seconds * maxlengthallbars * size / maxsec); //output string var uptimer; //creates output string and sends to gadget uptimer=""; uptimer += days; if (days == 1) { uptimer += " day, ";} else {uptimer += " days, ";} if (hours < 10) uptimer += "0"; uptimer += hours + ":"; if (minutes < 10) uptimer += "0"; uptimer += minutes + ":"; if (seconds < 10) uptimer += "0"; uptimer += seconds; uptimeTxt.innerText = uptimer; var secondOffset = secondTimeOffset(); uptimeTimeOut = setTimeout(uptime, secondOffset); } function secondTimeOffset(){ return 1000 - new Date().getMilliseconds(); }
But you should declare all local variables at the start of the function because the first thing the javascript engine supposedly does is find them all so it can determine scope.
You should try to use the var statement just once per function and separate the variable names with commas.
Also you should get into the habit of enclosing all the conditional statements in braces
eg instead of
if (seconds < 10) uptimer += "0";
write
if (seconds < 10) {uptimer += "0";}
so tidying it up a bit gives
function uptime(){ var now = new Date(), seconds, days, hours, minutes, uptimer="", secondOffset; seconds = (now - lastBootUpTime)/1000; days = Math.floor(seconds/60/60/24); seconds -= (days*24*60*60); hours = Math.floor(seconds/60/60); seconds -= (hours*60*60); minutes = Math.floor(seconds/60); seconds = seconds - (minutes*60); seconds = Math.floor(seconds); uptimer += days; if (days == 1) { uptimer += " day, ";} else {uptimer += " days, ";} if (hours < 10){ uptimer += "0";} uptimer += hours + ":"; if (minutes < 10){ uptimer += "0";} uptimer += minutes + ":"; if (seconds < 10){ uptimer += "0";} uptimer += seconds; uptimeTxt.innerText = uptimer; secondOffset = secondTimeOffset(); uptimeTimeOut = setTimeout(uptime, secondOffset); } function secondTimeOffset(){ return 1000 - new Date().getMilliseconds(); }
Good luck
- Edited by mystifeid Friday, September 14, 2012 12:00 PM


