Your issue lies with the fact that you are overwriting what valvonta refers to in this line:
var valvonta = 1;
This means that valvonta will no longer refer to the function itself, but 1 instead. Therefore your function fails after the first run. To fix this issue, simply use another variable name:
var vasen = 0;
(function valvonta() {
vasen++;
$('#valvonta').stop(true,true).css("left", vasen);
// Rename variables so they don't override `valvonta`
var v = 1;
var k = 12;
jQuery.post("ilmoittautuneet.php", {
valvonta: v,
kilpailu: k
}).done(function(data) {
});
setTimeout(valvonta, 6000);
})();
This solution, although functional, still makes it not very readable. What about simply calling a setInterval in the IIFE instead?
var vasen = 0;
(function () {
// Declare function to be called recursively
var valvonta = function() {
vasen++;
$('#valvonta').stop(true,true).css("left", vasen);
// Rename variables so they don't override `valvonta`
var v = 1;
var k = 12;
jQuery.post("ilmoittautuneet.php", {
valvonta: v,
kilpailu: k
}).done(function(data) {
});
};
// Recursively call function
setInterval(valvonta, 6000);
})();
Update: I have a feeling that you might want to call the function after the done callback of your AJAX. If that is the case, you will need to rework the logic above:
var vasen = 0;
(function () {
// Declare function to be called recursively
var valvonta = function() {
vasen++;
$('#valvonta').stop(true,true).css("left", vasen);
// Rename variables so they don't override `valvonta`
var v = 1;
var k = 12;
// Store AJAX promise in variable
var ajaxCall = jQuery.post("ilmoittautuneet.php", {
valvonta: v,
kilpailu: k
});
// When AJAX promise is resolved
ajaxCall.done(function() {
setTimeout(valvonta, 6000);
});
};
// Call function for the first time
valvonta();
})();