Count to target with JavaScript I'm looking for (and be as brutal as you like) ways to improve the code or the algorithm (I'm aware there should be comments) - I'm a recreational programmer and would like to be improving my skills. My next step is to get the targetNumber value from a nearby file without losing the current count.

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN "http://www.w3.org/TR/html4/strict.dtd"&gt; 
&lt;html&gt; 
&lt;head&gt; 
  &lt;title&gt;Count to a particular target&lt;/title&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;h1 id="myDiv"&gt;Starting&lt;/h1&gt; 
&lt;script type="text/javascript"&gt;
    currentValue = 100;
    targetValue = 1500;

    function count() {
        if (currentValue &gt; targetValue) {
        currentValue -= 1
        } else if (currentValue &lt; targetValue) {
            currentValue += 1
        } 
        document.getElementById('myDiv').innerHTML = 'Total wordcount:'+ currentValue.toString();
        changeTime = 20;
        if (Math.abs(currentValue - targetValue) &lt; 980) {
            changeTime = 1000 - Math.abs(currentValue - targetValue);
        }
        setTimeout(count,changeTime/2);
    }
count()
&lt;/script&gt; 
&lt;/body&gt; 
&lt;/html&gt;
function count(from, to, targetElem) {
    targetElem.innerHTML = 'Total wordcount: ' + from;

    if (from == to) return;

    from &lt; to ? from++ : from--; 

    var changeTime = Math.max(20, 1000 - Math.abs(from - to)) / 2;

    setTimeout(function() {count(from, to, target);}, changeTime);
}

count(50, 0, document.getElementById("t"));


A few notes:


avoid global variables to conserve state - use function arguments instead
always use var to declare variables. Otherwise they are in the global scope and you don't want this
added error checking (i.e. whether values are numeric and target is valid) might not be a bad idea, I left it out for clarity here
return early on known conditions, this reduces complexity in the function body 
the ternary operator condition ? ifTrue : ifFalse can be used in other ways than assigning a value