Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts

    sequential function call execution

    all,

    is there a way to run js functions one at a time? example, this is part of some code i have:
    Code:
    					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...

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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.

    Code:
    function test1 () {
      alert ("test1");
    }
    
    function test2 () {
      alert ("test2");
    }
    
    function test3 () {
      alert ("test3");
    }
    
    // runs in sequence
    test1 ();
    test2 ();
    test3 ();
    test2 ();
    test1 ();
    test2 ();
    Last edited by Trinithis; 07-19-2009 at 04:06 AM.
    Trinithis

  • #3
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Trinithis View Post
    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.

    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:
    Code:
    					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!
    Last edited by ajetrumpet; 07-19-2009 at 09:38 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    This should move you forward:-

    Code:
    <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>

  • Users who have thanked Philip M for this post:

    ajetrumpet (07-19-2009)

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    How about something like this? (Untested)
    Code:
    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 ();
    			}
    		};	
    	}) ()
    };
    Code:
    var sequencer = new DelaySequencer ();
    sequencer.seq (function () {EvalSound ("CorrectIs")}, 3000);
    // ...
    Trinithis

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Trinithis - Way over my head, I am afraid. What does your script do better/more effectively in 30 lines that my suggestion does in 6?

  • #7
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Philip M View Post
    This should move you forward:-

    Code:
    <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.

  • #8
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    philip,

    i have used your code successfully in all but one place. i have this code now:
    Code:
    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:
    Code:
    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....

  • #9
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Quote Originally Posted by Philip M View Post
    Trinithis - Way over my head, I am afraid. 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:
    Code:
    var timeOffset = 0;
    setTimeout (takes1seconds, timeOffset);
    timeOffset += 1000;
    setTimeout (takes4seconds, timeOffset);
    timeOffset += 4000;
    setTimeout (takes2seconds, timeOffset);
    timeOffset += 2000;
    // etc
    With Delay Sequencer:
    Code:
    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:
    Code:
    seq (func)
    seq (preDelay, func)
    seq (func, postDelay)
    seq (preDelay, func, postDelay)
    Respectively, they get 'translated' into the following:
    Code:
    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.
    Trinithis

  • #10
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by ajetrumpet View Post
    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.

  • #11
    Regular Coder ajetrumpet's Avatar
    Join Date
    Jul 2009
    Location
    Iowa City, IA
    Posts
    407
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by fside View Post
    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •