How to generate a four digit code (i.e 0001) javascript
<p>I need to autogenerate a new four digit code that everytime I run the function the value increments. Such as 0001 the first time, 0002, 0003 and so on.
This is the code I have so far, the format is okay but I cant get it to increment the value automatically. I know it's probably a while loop but I wanted to ask advise on how it should be done. Thank you.</p>

<pre><code>function pad(n){

var num = 1;
var string = "" + num;
var pad = "0000";
n = pad.substring(0, pad.length - string.length) + string;
num++;
return n;
}
</code></pre>