dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: stopping a function (typewriter)

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Posts
    172

    Exclamation stopping a function (typewriter)

    I have a function that writes a text to a textspace, with a typewriter effect.
    This is the function:

    Code:
    	function writeContent1() {
    		var str:String = "Lorem ipsum dolor sit amet, doming sequitur, claram futurum consectetuer seacula nam id aliquam molestie ipsum. Parum facer demonstraverunt non ii litterarum imperdiet nibh delenit. Duis consuetudium videntur eum liber processus ut typi laoreet. Decima facilisi nunc doming luptatum per autem formas, clari luptatum accumsan. In et vulputate qui minim, futurum nam lectores, usus nisl eorum. Consequat humanitatis praesent lobortis facer molestie eum aliquam consequat, at. Nostrud veniam vulputate mirum lorem, ad est feugiat eu, iusto nostrud. Suscipit facit per quarta decima tempor. Consectetuer seacula clari eua nobis sed volutpat te duis littera vel. Lectorum, saepius me tincidunt, anteposuerit cum erat imperdiet ex. Facit luptatum te eum notare decima futurum diam. Typi sed exerci me dignissim legentis. Est non at clari praesent, congue ad elit investigationes. Cum molestie fiant eua quod et eros eu id vel vulputate. Lobortis exerci, legunt ii in ex anteposuerit nobis, anteposuerit notare. Nibh usus sed nam, te legere videntur me. Est parum sit non at vero dolor quis per. Ad cum notare, investigationes eodem modo habent praesent. Nobis blandit eua et mutationem anteposuerit quarta eu claritas. Nulla nostrud seacula facilisi feugiat. Commodo erat sequitur liber possim. Fiant placerat id typi, ii per processus duis in mirum. Consequat mutationem demonstraverunt ex sit augue te mazim nunc.";
    
    		var sInt = setInterval(writeIt,1);
    		var count:Number = 0;
    
    
    		function writeIt() {
    			c1txt.text = str.substring(0,count);
    			count += 5;
    			if (count > str.length+5) {
    				clearInterval(sInt);
    			}
    		}
    	}

    but when I try to re-execute the function or change the text directly (while the function is still continuing) it messesthings up. Nothing happens, or it really messes up.

    What can I do to correct this problem ? I thought I have to stop the function first to write the other text.


    Well, what I want to do is, to have a function like this

    Code:
    typewrite ("my text here");
    And I want it to typewrite the text to c1txt.text

    I hope I'm clear,
    thank you.

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Code:
    var str:String = "Lorem ipsum dolor sit amet, doming sequitur, claram futurum consectetuer seacula nam id aliquam molestie ipsum. Parum facer demonstraverunt non ii litterarum imperdiet nibh delenit. Duis consuetudium videntur eum liber processus ut typi laoreet. Decima facilisi nunc doming luptatum per autem formas, clari luptatum accumsan. In et vulputate qui minim, futurum nam lectores, usus nisl eorum. Consequat humanitatis praesent lobortis facer molestie eum aliquam consequat, at. Nostrud veniam vulputate mirum lorem, ad est feugiat eu, iusto nostrud. Suscipit facit per quarta decima tempor. Consectetuer seacula clari eua nobis sed volutpat te duis littera vel. Lectorum, saepius me tincidunt, anteposuerit cum erat imperdiet ex. Facit luptatum te eum notare decima futurum diam. Typi sed exerci me dignissim legentis. Est non at clari praesent, congue ad elit investigationes. Cum molestie fiant eua quod et eros eu id vel vulputate. Lobortis exerci, legunt ii in ex anteposuerit nobis, anteposuerit notare. Nibh usus sed nam, te legere videntur me. Est parum sit non at vero dolor quis per. Ad cum notare, investigationes eodem modo habent praesent. Nobis blandit eua et mutationem anteposuerit quarta eu claritas. Nulla nostrud seacula facilisi feugiat. Commodo erat sequitur liber possim. Fiant placerat id typi, ii per processus duis in mirum. Consequat mutationem demonstraverunt ex sit augue te mazim nunc.";
    var txtHold:TextField;
    var strHold:String;
    var count:Number;
    function writeContent1(strvar:String, txtf:TextField) {
         txtHold = txtf;
         strHold = strvar;
         count = 0;
         var sInt = setInterval(writeIt,1);
    }
    function writeIt() {
         txtHold.text = str.substring(0,count);
         count += 5;
         if (count > strHold.length+5) {
    	clearInterval(sInt);
         }
    }
    writeContent1(str, c1txt);
    Try that. I'm not real sure your problem, but this may help, not sure though. let me know.
    Last edited by jweeks123; 10-22-2008 at 12:58 PM.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    172
    I'll try this in a little time ! Thank you !

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    No Problem, hope it helps.

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    172
    I edited your function as this one, it works now:

    Code:
    var txtHold:TextField;
    var strHold:String;
    var count:Number;
    function writeContent1(strvar:String, txtf:TextField) {
    	txtHold = txtf;
    	strHold = strvar;
    	count = 0;
    	var sInt = setInterval(writeIt,3);
    	function writeIt() {
    		txtHold.text = strHold.substring(0,count);
    		count += 1;
    		if (count > strHold.length) {
    			clearInterval(sInt);
    		}
    	}
    
    }
    
    
    writeContent1("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam consequat. Integer adipiscing mollis nisl. Mauris mattis nunc posuere tortor. Duis id lorem. Quisque accumsan nisl volutpat ipsum. In iaculis tellus sit amet metus. Curabitur placerat interdum nunc. Vestibulum purus felis, commodo ac, condimentum a, condimentum eu, lacus. Etiam vehicula, nisi et faucibus vulputate, justo quam venenatis mauris, vel tempor elit augue vel augue. Morbi mollis vestibulum pede. Cras pulvinar justo vitae quam. Sed pulvinar posuere risus. Praesent eu purus. Nulla luctus eros nec justo. Maecenas egestas pulvinar libero. Fusce magna mi, pellentesque feugiat, mollis vel, placerat faucibus, nulla. Cras bibendum, lorem et egestas auctor, ipsum massa tempor lacus, et aliquet est tortor in sapien. Aliquam vel arcu. Suspendisse potenti. Quisque tellus. Aliquam justo leo, pulvinar ac, vehicula at, pretium sit amet, dui. Aenean mi libero, blandit sed, feugiat ac, congue in, erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Duis est elit, venenatis non, consectetuer at, laoreet sit amet, ante. Quisque convallis ante quis mi. Praesent fermentum. Proin rhoncus. Donec risus eros, cursus id, aliquam eu, egestas id, nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla et augue. Curabitur at diam id purus suscipit sollicitudin. ", c1txt);

    but the problem is, every time I click the button, it goes faster and faster.
    That was what I meant by "stopping the function"
    I guess it re-starts everytime, and if it is not stopped previopusly, it adds the "writeit" function again and again so that it goes faster each time.

    I need to learn some way to correct this.

  6. #6
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    1) Don't use setInterval, use Timer instead.
    2) It's going faster and faster because with each click you are initiating that setInterval function that keeps on firing the writeIt(); function. You are never stopping the interval so with every click you're adding +1 setInterval ( meaning that you'll have +1 setInterval calling the same writeIt function, resulting into faster and faster calls ).

    In order to make it work correctly, you first have to stop the setInterval if it exists and only after that start it. Btw, don't nest functions because it's really not cool and it's not a good practice.
    Last edited by fx.barrett; 10-23-2008 at 09:16 AM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center