...

View Full Version : sequential function call execution



ajetrumpet
07-19-2009, 03:23 AM
all,

is there a way to run js functions one at a time? example, this is part of some code i have:
EvalSound('CorrectIs');
WrongAnswer();
PlaysRemain();
DisableCheckAnswer();
checkAnswerButton('checkanswer2');
document.getElementById('answer').innerHTML = Answer1;it works okay, but all of the functions run at the same time when the code gets to this point. is there an attribute or something that will allow me to run these functions one at a time? i would really like to defer the next function in the sequence from executing until the previous has completely finished executing.

this type of thing is automatic in VB, which is what i usually program in. thank you for any help...

Trinithis
07-19-2009, 04:59 AM
The code DOES run in sequence. The only issue I can fathom is if one of the functions you have there call setTimeout, setInterval, or some AJAX code.



function test1 () {
alert ("test1");
}

function test2 () {
alert ("test2");
}

function test3 () {
alert ("test3");
}

// runs in sequence
test1 ();
test2 ();
test3 ();
test2 ();
test1 ();
test2 ();

ajetrumpet
07-19-2009, 10:36 AM
The code DOES run in sequence. The only issue I can fathom is if one of the functions you have there call setTimeout, setInterval, or some AJAX code.



function test1 () {
alert ("test1");
}

function test2 () {
alert ("test2");
}

function test3 () {
alert ("test3");
}

// runs in sequence
test1 ();
test2 ();
test3 ();
test2 ();
test1 ();
test2 ();
i'm sure code like that would run in sequence, because those alert boxes are modal popup forms. (i think). but what about something like this:
EvalSound('CorrectIs');
document.getElementById('answer').innerHTML = Answer1;those are two lines that are executing in sequence i'm sure, but it takes place so fast that it seems like simultaneously. the EvalSound() function plays a 3 second wave file, and the other line shows an answer to question (header text - level 1) in an answer box div. what i am specifically wanting in this case is the answer not to appear in the answer box until the wave file is finished playing. is this a special case? i have multiple instances of this type of situation in my page code, and it all needs this type of fix, if it's possible...

thanks!

Philip M
07-19-2009, 12:46 PM
This should move you forward:-


<div id = "answer"></div>

<script type = "text/javascript">

var answer1 = "The answer is dog";
var answer2 = "The answer is cat";

EvalSound('CorrectIs');
tim = setTimeout("showAnswer(1)", 3000); // 3 seconds delay

function showAnswer (x) {
var ansToShow = "answer" + x;
document.getElementById('answer').innerHTML = eval(ansToShow);
}

</script>

Trinithis
07-19-2009, 08:12 PM
How about something like this? (Untested)


function DelaySequencer (initDelay) {
initDelay = initDelay ? initDelay : 0;
this.delays = [initDelay];
this.funcs = [];
this.seq (function () { /*empty*/ });
}

DelaySequencer.prototype = {
run: function () {
var self = this;
setTimeout (function () {
self.funcs.shift ();
self.run ();
}, this.delays.shift ());
},
seq: (function () {
var seq = function (preDelay, func, postDelay) {
this.delays [this.delays.length - 1] += preDelay;
this.delays.push (postDelay);
this.funcs.push (func);
};
return function () {
var initRun = this.funcs.length == 0;
switch (arguments.length) {
case 1:
seq (0, arguments [0], 0);
break;
case 2:
if (typeof arguments [0] == "function") {
seq (0, arguments [0], arguments [1]);
break;
}
else {
seq (arguments [0], arguments [1], 0);
break;
}
default:
seq.apply (null, arguments);
break;
}
if (initRun) {
this.run ();
}
};
}) ()
};



var sequencer = new DelaySequencer ();
sequencer.seq (function () {EvalSound ("CorrectIs")}, 3000);
// ...

Philip M
07-19-2009, 08:23 PM
Trinithis - Way over my head, I am afraid. :o What does your script do better/more effectively in 30 lines that my suggestion does in 6?

ajetrumpet
07-19-2009, 10:13 PM
This should move you forward:-


<div id = "answer"></div>

<script type = "text/javascript">

var answer1 = "The answer is dog";
var answer2 = "The answer is cat";

EvalSound('CorrectIs');
tim = setTimeout("showAnswer(1)", 3000); // 3 seconds delay

function showAnswer (x) {
var ansToShow = "answer" + x;
document.getElementById('answer').innerHTML = eval(ansToShow);
}

</script>i adapted this to fit my needs. thanks a bunch! I really do appreciate it. as i said, i'm not very experienced yet. :)

ajetrumpet
07-20-2009, 12:28 AM
philip,

i have used your code successfully in all but one place. i have this code now:
tim = setTimeout("document.getElementById('apDiv8').style.backgroundColor = Blue", 2500);it is within another function. "Blue" is a string variable. the div color is not changing after 2.5 seconds. however, i do have another line right after that one. it is this:
tim = setTimeout("EvalSound('PlayAgain')", 5000);could that be a conflict? i had it working at one pt, now i can't seem to find the magic agian....

Trinithis
07-20-2009, 04:14 AM
Trinithis - Way over my head, I am afraid. :o What does your script do better/more effectively in 30 lines that my suggestion does in 6?

If you had to have multiple delays, mine (assuming no bugs) will handle that more easily, I think.

Without Delay Sequencer:


var timeOffset = 0;
setTimeout (takes1seconds, timeOffset);
timeOffset += 1000;
setTimeout (takes4seconds, timeOffset);
timeOffset += 4000;
setTimeout (takes2seconds, timeOffset);
timeOffset += 2000;
// etc


With Delay Sequencer:


var sequencer = new DelaySequencer ();
sequencer.seq (takes1seconds, 1000);
sequencer.seq (takes4seconds, 4000);
sequencer.seq (takes2seconds, 2000);


I suspect most of the complexity in my code comes from the fact that I 'overloaded' the seq method. Basically there are four versions of it:


seq (func)
seq (preDelay, func)
seq (func, postDelay)
seq (preDelay, func, postDelay)

Respectively, they get 'translated' into the following:


seq (0, func, 0)
seq (preDelay, func, 0)
seq (0, func, postDelay)
seq (preDelay, func, postDelay) // stays the same... master version


Use whatever method you want. I was just killing some time.

fside
07-20-2009, 05:07 AM
philip,

i have used your code successfully in all but one place. i have this code now


I think the problem is that you didn't describe your problem in the OP. A LOT of people posting here seem extremely reluctant to simply state in the clearest language possible just exactly what it is they are trying to do. Trin was right in saying that Javascript executes synchronously. Had you said, instead, that you want a delay, a pause, for some effect to happen or music to play, the answer would have been that - you are attempting some animation. And that requires time outs.

And then there are a number of approaches. If you try to solve the immediate problem with a quick fix, knowing that the real problem is that you need something to handle animations, you could run into problems if you try to expand or change that animation.

Trin suggested something of a general handler for setting the necessary delays. That's really all you want, isn't it? And if you are actually waiting for someone to complete a field, or signal complete with a button or otherwise, that itself is yet a different problem.

It sounds even like you might benefit from a simple CAI package, and surely there must be simple plugins for this for jQuery.

ajetrumpet
07-20-2009, 05:22 AM
I think the problem is that you didn't describe your problem in the OP. A LOT of people posting here seem extremely reluctant to simply state in the clearest language possible just exactly what it is they are trying to do. Trin was right in saying that Javascript executes synchronously. Had you said, instead, that you want a delay, a pause, for some effect to happen or music to play, the answer would have been that - you are attempting some animation. And that requires time outs.

And then there are a number of approaches. If you try to solve the immediate problem with a quick fix, knowing that the real problem is that you need something to handle animations, you could run into problems if you try to expand or change that animation.

Trin suggested something of a general handler for setting the necessary delays. That's really all you want, isn't it? And if you are actually waiting for someone to complete a field, or signal complete with a button or otherwise, that itself is yet a different problem.

It sounds even like you might benefit from a simple CAI package, and surely there must be simple plugins for this for jQuery.my last post was a problem a fixed by myself. it was a simple concat issue using the + sign. thank you for the explanation...i appreciate it, but i really am not experienced in javascript. i am no expert in javascript, but i hope to improve someday. thanks for all the help in this thread every1...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum