# Need Help With Coding



## cubernya (Aug 24, 2011)

Alright, I'm back again, and need some more help. This time I'm trying to code a very simple timer, not even a cubing timer (although I might make it one). I can't seem to get the timer to stop at all.

Source code:

```
<script type='text/javascript'>
function start(){
	window.int = setInterval('timer()',10);
	document.getElementById('startstop').onkeydown = 'stop()';
	document.getElementById('startstop').value = 'STOP';
}
function stop(){
	int = clearInterval(int);
	document.getElementById('startstop').onkeydown = 'start()';
	document.getElementById('startstop').value = 'START';
	var t = document.getElementById('times').innerHTML;
	var ct = document.getElementById('sec').innerHTML + document.getElementById('frac').innerHTML;
	document.getElementById('times').innerHTML = t + ' ,' + ct;
}
function timer(){
	// Define variables
	var frac = Math.round(document.getElementById('frac').innerHTML);
	var sec = Math.round(document.getElementById('sec').innerHTML);
	
	// Change decimal and second
	frac = frac + 1;
	if(frac <= 9){
		frac = '0' + frac;
	}
	else if(frac == 100){
		frac = '00';
		sec = sec + 1;
	}

	// Change time displayed
	document.getElementById('frac').innerHTML = frac;
	document.getElementById('sec').innerHTML = sec;
}
</script>

<table width='1000' height='450'><tr>
<td valign='middle' align='center'>

<font size='7'><span id='time' onkeyup='start()'><span id='sec'>0</span>.<span id='frac'>00</span></span></font>
<br/>
<input type='text' value='START' onkeydown='start()' maxlength='0' size='12' id='startstop'/>
<br/><br/>
<span id='times'></span>
</td>
</tr></table>
```

As you can see, under stop(), I put the clear.interval. I've tried putting in window.clear.interval and/or window.int = ... but none have worked. I was just wondering how I should go about getting it to stop in Firefox. This is actually for a project, and I need a timer to show in the corner, but don't want something larger like qqTimer.


----------



## Kirjava (Aug 24, 2011)

You left some <div> tags open before <div id="fw-sidebar"> and need to close them.

my paypal address is [email protected]

np


----------



## cubernya (Aug 24, 2011)

Could you show me where I would need to insert the </div>? It might be in a sidebar module I took from PayPal's code...

EDIT: I see where you mean to insert it. Just at the end of the second view cart button, correct?


----------



## Kirjava (Aug 24, 2011)

After <div class="fw-paragraphbottom">.


----------



## cubernya (Aug 24, 2011)

Hmm...weird

Now to try and figure out how to get that inserted (and how a </div> got removed)


----------



## cubernya (Aug 24, 2011)

Just did a non-live version of the webpage by inserting it (anywhere from 1-3 </div>s) and all it did was add in the background colour for the sidebar, but nothing else


----------



## Kirjava (Aug 24, 2011)

Eh, I had it working fine before.

I'll send you a fixed copy when I get home.


----------



## cubernya (Aug 24, 2011)

Alright. After I receive the fixed copy (and hopefully how to insert it with webs) I'll send you the $5


----------



## Kirjava (Aug 24, 2011)

http://www.speedyshare.com/files/30014894/products.htm


----------



## cubernya (Aug 24, 2011)

Lol talk about broken code


----------



## Kirjava (Aug 24, 2011)

****ing firefox, gimmie a sec


----------



## Kirjava (Aug 24, 2011)

http://www.speedyshare.com/files/30015169/test.html

there, fixed.

firefox wanted to change all the direct links.


----------



## cubernya (Aug 24, 2011)

So now the question remains...was this my coding fault or was it the site I'm using?

(and how to insert the </div> tags without replacing everything)


----------



## Kirjava (Aug 24, 2011)

no idea. your code is quite the cluster****


----------



## cubernya (Aug 24, 2011)

Yeah it's no where near like that when I'm editing it  Anything I code is nice and organized

That's just what the site puts up


----------



## cubernya (Dec 6, 2011)

Updated first post with a new problem


----------



## joey (Dec 6, 2011)

```
var int;
function start(){
	int = setInterval('timer()',10);
}
function stop(){
	clearInterval(int);
}
```


----------



## cubernya (Dec 6, 2011)

Although I did set var int outside a function, it still doesn't work 

In case you want a live version of this timer, please go here


----------



## cubernya (Dec 7, 2011)

Can anybody help me with this?


----------



## PatrickJameson (Dec 7, 2011)

My quick one: http://dl.dropbox.com/u/922232/test2.html
Your "fixed" one: http://dl.dropbox.com/u/922232/z.html


----------



## cubernya (Dec 7, 2011)

Thanks Pat, worked wonderfully. I also finally changed it to make it reset to 0 after each time (derp) and add in the Time: x part. This made it look a little better. I tried to get it to start by clicking on the text first (setting focus) like yours, but then whenever I hit the start button (aka any key) it would reset to 0 and double up, not putting a time down. That's the next thing I want to conquer


----------



## cubernya (Feb 12, 2012)

Bump

Working on a different version of timer, and I have some problems (as expected), but I can't figure out how to solve them.

Why the avgX(x) function isn't working on my timer? I've been trying to change approaches, but I've gotten no where

How can I make it so that when I'm sorting the array, it will treat a DNF as the highest time (number)?

Note: I want to keep it so it's just JavaScript, so please try and keep it that way if you can.


----------



## maderito (Feb 12, 2012)

theZcuber said:


> Bump
> 
> Why the avgX(x) function isn't working on my timer? I've been trying to change approaches, but I've gotten no where



I see at least 2 instances where "avgX()" is written "avgx()".


----------



## cubernya (Feb 12, 2012)

maderito said:


> I see at least 2 instances where "avgX()" is written "avgx()".


 
I just used the find function in notepad, and it came up in one place (the penalty function). It still wouldn't make sense why it isn't working otherwise, since I wasn't even testing it with the penalties. I've fixed it though, and it still doesn't work


----------



## maderito (Feb 12, 2012)

You have "avg5()" in your code. Do you mean "avgX(5)"?

edit: Also, "total" is not defined in your avgX function.


----------



## cubernya (Feb 12, 2012)

maderito said:


> You have "avg5()" in your code. Do you mean "avgX(5)"?
> 
> edit: Also, "total" is not defined in your avgX function.


 
It was originally meant to be just an avg5 function, but then I realized I could easily alter it so it would be avg of anything. I guess I forgot to change a couple things.

I've just defined total, but it still doesn't work


----------



## maderito (Feb 12, 2012)

There are good developer tools for debugging in the Chrome web browser, including a Javascript Console. I'm not a Javascript programmer, but your code looks straightforward. You need a debugger if you're not using one. 

You have at least 2 average functions, average() and avgX(). "total" is still not defined in one of them (if I'm seeing your current code).


----------



## cubernya (Feb 12, 2012)

The average function has total, and works perfectly. The avg function has total defined, but doesn't work. Are you viewing ztimer.tk's source? That is the most current version, and has it defined in both functions.

Also, how did you know I was using chrome?


----------



## maderito (Feb 12, 2012)

theZcuber said:


> Also, how did you know I was using chrome?



Doesn't everyone 

So you have chrome on your PC -- use the javascript console - it'll will point out some errors, especially un-initialized variables. I'll take another look at the code later today.


----------



## cubernya (Feb 12, 2012)

I got it to take the first 5 solves, but for some reason it won't change to take the last 5 solves. I tried reverse() for the array, but that doesn't fix it.


----------



## maderito (Feb 12, 2012)

Your avgX() function needs reworking. In the loop below, the variable "times" (originally an array) is set to a new value (and possibly no value) each iteration of the loop. Shift returns a value from an array, not an array. 

for (i=0; times.length > x; i++){
var times = times.shift(); }

Also, I'm not sure you've set up your number sort function correctly. See this and set up the numerical sort method exactly as shown.

And there may be other problems in the function - I'm sure you'll figure it out.


----------



## cubernya (Feb 12, 2012)

maderito said:


> Your avgX() function needs reworking. In the loop below, the variable "times" (originally an array) is set to a new value (and possibly no value) each iteration of the loop. Shift returns a value from an array, not an array.
> 
> for (i=0; times.length > x; i++){
> var times = times.shift(); }
> ...


 
Oops, I forgot that x.shift() only returns the number 
The number function is set up just as it should be, it's just that the needed function is at the top of the code, and only on one line (so it's hard to find )

I just changed it, and the average itself still works great. It's just that it will not take the last 5, but rather the first 5 times.


----------



## cubernya (Feb 13, 2012)

I forget what I did to make it work (I did it during school), but it works now! I've also added in an avg12, 50, and 100, so that's great!

Now how do I make it so that when I sort the array, it treats DNF as the worst (highest) number?

Again, the URL is http://ztimer.tk/


----------



## cubernya (Feb 21, 2012)

Alright. Now that I have the avgX function fully working, I've been trying to debug other things, which are listed below.

When sorting numbers, DNF is not treated as the worst time.
The scramblers are not working (3-11 are custom made, Megaminx, Pyraminx, and Clock WCA, so I'm not sure why)

After I get these few things working, then I'll attempt to make it so you can add comments, as well as adding an inspection feature (with automatic +2s and DNFs). After I get that, then I'll officially release it!


----------



## Stefan (Feb 21, 2012)

Am I imagining things, or are you bumping this thread by deleting and reposting that post over and over again?


----------



## cubernya (Mar 5, 2012)

Progress over the last day is amazing. Not only did I implement the inspection (with auto +2s/DNFs), I also had a very simple fix to sort the DNFs at the end. While doing that, the scramblers I've coded have magically fixed themselves (other than the ones that weren't working anyway, like the Mega and Pyra).

If you have any requests for things to implement into the timer, ask soon, as it'll be completely released after I get the scramblers working!


----------



## cubernya (Jun 5, 2012)

Does anybody know if, in Javascript, it's possible to use a variable when requesting another variable?

Example:
var cid = 1
var cname = cid*(cid value)*[1]

Is this possible, or am I SOL


----------



## Lucas Garron (Jun 6, 2012)

theZcuber said:


> Does anybody know if, in Javascript, it's possible to use a variable when requesting another variable?
> 
> Example:
> var cid = 1
> ...



Yes, it is, but it's generally considered a bad idea. If you can, you should try to write your code so this is not necessary.


----------



## cubernya (Jul 4, 2012)

Could somebody look this over? I'm not too sure why, especially considering I just rewrote the line that's throwing the error. Here's the error that Chrome is giving me: Uncaught SyntaxError: Unexpected token ; 

Link: http://cubesites.webs.com/rpgbeta.html


----------

