/    Sign up×
Community /Pin to ProfileBookmark

Need Help Understanding Small Script

I found this script on a website and I’m trying to change it up a little bit and see if I can get some usability out of it. Of course, I will give credit to the place I found it. So, can anyone help me understand this script a bit more? I would like to be able to change up the amount of change each second. Currently, it looks like .0004 per second. I would also like to be able to set the starting value. (Like where did the counter start?)

Any help would be greatly appreciated.

[CODE]<html>
<head>

<script type=”text/javascript”>
<!–

function element(id) {
if (document.getElementById) {
return document.getElementById(id);
} else if (window[id]) {
return window[id];
}
return null;
}

var COUNT = [
[ 1171429200000, 102400 ],
[ 1181797200000, 106496 ]
];

var storage;

function updateStorage() {
if (!storage) {
return;
}

var current = (new Date()).getTime();
var i;
for (i = 0; i < COUNT.length; i++) {
if (current < COUNT[i][0]) {
break;
}
}
if (i == 0) {
setTimeout(updateStorage, 1000);
} else if (i == COUNT.length) {
storage.innerHTML = COUNT[i – 1][1];
} else {
var ts = COUNT[i – 1][0];
var bs = COUNT[i – 1][1];
storage.innerHTML = formatStorage(((current-ts) / (COUNT[i][0]-ts) * (COUNT[i][1]-bs)) + bs);
setTimeout(updateStorage, 1000);
}
}

var PAD = ‘.0000’;

function formatStorage(num) {
var str = String(num);
var dot = str.indexOf(‘.’);
if (dot < 0) {
return str + PAD;
} if (PAD.length > (str.length – dot)) {
return str + PAD.substring(str.length – dot);
} else {
return str.substring(0, dot + PAD.length);
}
}

// –>
</script>
</head>

<body>
Storage Counter: <b id=”storage”></b>&nbsp;MB
<script type=”text/javascript”>
<!–
storage = element(“storage”);
updateStorage();
// –>
</script>
</body>
</html>[/CODE]

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoMar 05.2007 — If it was me I would scrap that code and find a better one (acually I would write a better one). There are a lot of errors in it. For example, your loop has

i<COUNT.length

and then in it it checks:

else if (i == COUNT.length)

which will never happen, since the loop wont run if i==COUNT.length

As far as the time goes, it is every second:

setTimeout(updateStorage, 1000);

1000 = 1000 milliseconds = 1 second
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — I thought I saw something a little messed up with this code. Anyways, the value seems to increase by .0004 every second. Could you help me change that? Or even help me come up with an entire new script? I understand a little bit of JavaScript; this is just barely over my head.

Thanks for the help.
Copy linkTweet thisAlerts:
@konithomimoMar 05.2007 — The amount to change each time is in your COUNT array, as the second element of each inner array:

var COUNT = [

[ 1171429200000, 102400 ],

[ 1181797200000, 106496 ]

];

that means that 102400 will be the first change, and 106496 will be the second change, and then it goes back and forth. To increase it change the numbers.
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — Alright. Thanks.

What do the numbers 1171429200000 and 1181797200000 mean?
Copy linkTweet thisAlerts:
@Jeff_MottMar 05.2007 — There are a lot of errors in it. For example, your loop has

i<COUNT.length

and then in it it checks:

else if (i == COUNT.length)

which will never happen, since the loop wont run if i==COUNT.length[/quote]
That is actually not an error. The loop that you're referring to will "break" at some point during the loop. Thus, the if statement is checking whether the loop ran to the end or not.
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — I guess that's makes a little more sense, but still what do those numbers mean? (1171429200000 and 1181797200000)
Copy linkTweet thisAlerts:
@Jeff_MottMar 05.2007 — Those numbers represent dates. They are milliseconds that have passed since 1 January 1970 00:00:00.

You can see what dates they represent:alert( new Date(1171429200000).toLocaleString() );
alert( new Date(1181797200000).toLocaleString() );
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — Alright, I understand that know, but how would I alter this script to, say, increase by .0003 every second and not .0004. Do you know what I mean? Like,...how could I change the value so that it's not 104910.56347 MB. Could I make it smaller and increase slower?

Thanks again for that clarification.
Copy linkTweet thisAlerts:
@konithomimoMar 05.2007 — That is actually not an error. The loop that you're referring to will "break" at some point during the loop. Thus, the if statement is checking whether the loop ran to the end or not.[/QUOTE]

THe loop will not run if i==COUNT.length, since it runs for when i<COUNT.length

To see if the loop is on the last iteration it would be:

if(i==COUNT.length-1)

Read up on loops. The code contained within the loop will not run if the conditions for the loop are not met, as would be the case if i==COUNT.length
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — I'm not sure, but whatever the case...the script works. I just need the clarification on how to change the values.
Copy linkTweet thisAlerts:
@Jeff_MottMar 05.2007 — THe loop will not run if i==COUNT.length, since it runs for when i<COUNT.length

To see if the loop is on the last iteration it would be:

if(i==COUNT.length-1)[/quote]
But the [font=courier]i == COUNT.length[/font] is not within the loop. After the loop has run, [font=courier]i[/font] will be equal to [font=courier]COUNT.length[/font] if the [font=courier]break[/font] statement was never called. If the looped was [font=courier]break[/font]ed, then [font=courier]i[/font] will be less than [font=courier]COUNT.length[/font].

You can try it for yourself:var countTo = 10;


for (var i = 0; i &lt; countTo; i++)
/* no break */;
if (i == countTo)
alert("I counted all the way to ten!");
else
alert("I didn't make it.");


for (var i = 0; i &lt; countTo; i++)
if (i == 5) break; /* a break in the middle of the loop */
if (i == countTo)
alert("I counted all the way to ten!");
else
alert("I didn't make it.");


Read up on loops. The code contained within the loop will not run if the conditions for the loop are not met, as would be the case if i==COUNT.length[/quote]I know all I need to know about loops, as I'm sure you do too. But you have misread the code that was posted here. The [font=courier]i==COUNT.length[/font] if statement is [i]not[/i] within the loop.
Copy linkTweet thisAlerts:
@Jeff_MottMar 05.2007 — Paintball24, the second items in the COUNT array seem to represent byte values.

var COUNT = [
[ 1171429200000, [u]102400[/u] ],
[ 1181797200000, [u]106496[/u] ]
];


I can't tell you exactly how to change them, because the formula that uses these numbers seems a bit obscure. But you can tweak those numbers until you get the result you want.
Copy linkTweet thisAlerts:
@Paintball24authorMar 05.2007 — Thanks for the help.

I've been messing around with those numbers for quite some time now and I still can't seem to produce the wanted results. The numbers seem to have no relationship with each other. Can you help steer me in the right direction?
Copy linkTweet thisAlerts:
@Jeff_MottMar 06.2007 — Again, I don't know the intricacies of the formula used, but I have discovered that if I add three zeros to the end of each number, then the counter increments about 0.4 MB each time.

var COUNT = [
[ 1171429200000, 102400[u]000[/u] ],
[ 1181797200000, 106496[u]000[/u] ]
];


So I'm afraid that the best advice I can give is still the same: Tweak those numbers until it counts at the rate you would like.
Copy linkTweet thisAlerts:
@Paintball24authorMar 06.2007 — It seems like I can't. If I make the total value smaller, then it counts way too slow.

Could someone provide me with a script, or a link to a script, that can do this same sort of thing while still maintaining control?
×

Success!

Help @Paintball24 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...