<script> function next() { var max = 9999, i = -1; return function () { i = i < max ? i + 1 : 0; return i; } } var new_id = next(); alert(new_id()); alert(new_id()); </script>