Solved

How to interrupt javascript code?

Posted on 2001-08-16
13
2,106 Views
Last Modified: 2012-06-21
Hi!
I have the following code:
...
var Stopped = false;

function doSomething()
{
     Stopped = false;
     while(!Stopped) {
            // do something
     }
}

function Stop()
{
     Stopped = true;
}
...
Function "doSomething" is started when user clicks on a button "DO". After that, javascript code runs and the browser hangs. I would like to keep browser alive - so the user to be able to click on button "stop" that will call "stop" function that will stop "doSomething".
Is it possible to do that and how?

Thanks.
0
Comment
Question by:djokov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 14

Expert Comment

by:avner
ID: 6393470
a completly different approach should be :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
//[avnerc@fundtech.co.il]
window.status=1
aa1=function(){aa=window.status;aa++;window.status=aa};
setInterval("aa1()",200);


//-->
</SCRIPT>
</HEAD>

<BODY>
</BODY>
</HTML>
0
 
LVL 14

Expert Comment

by:avner
ID: 6393473
you can also do :

moshe=setInterval("aa1()",200);

and then when you want to stop the repeating method :

clearInterval(moshe)


0
 
LVL 2

Author Comment

by:djokov
ID: 6393592
second approach is good.
however, i have arguments that I have to pass to aa() function. In javascript documentation is written that it should be done this way:

function aa(a, b)
{
...
}

function blabla(a1, b1)
{
moshe=setInterval("aa1()",200, a1, b1);
}

but it is doesn't work. if i try

moshe=setInterval("aa1(a1, b1)",200); it gaves error
if try
moshe=setInterval("aa1(1, 1)",200); it works

but i need to pass a1 and b1. how?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 14

Expert Comment

by:avner
ID: 6393631
Change the method :

moshe=0
setInterval("aa1()",200);


function aa1()
{
window.status=moshe;
moshe++;
}


This way , when the moshe is a global param , you don't need to send it.


0
 
LVL 2

Author Comment

by:djokov
ID: 6393658
no way,
i know you are expert in XML also and you will understand.. :-)) this functions are connected with some data from xml files some of them are dynamicly genereted... etc.etc, i just need to pass this parameters. as i see in documentation, it should be possible, but i don't know why it doesn't run at my side.
0
 
LVL 14

Expert Comment

by:avner
ID: 6393670
Yes it should be.. it just some times a bit buggy ...
Let me try something, how about :


You said that this :

moshe=setInterval("aa1(a1, b1)",200); it gaves error

dosen't work , but where do you actually declar the values a1,b1 ?


0
 
LVL 14

Expert Comment

by:avner
ID: 6393678
if you'll try :

a="moshe"
setInterval("aa1(a)",200);


function aa1(a1)
{
alert(a1);
}


the "moshe" will display , that means the variable is being passed.
0
 
LVL 2

Author Comment

by:djokov
ID: 6393703
No, see...
now i have this:

function blabla(a1, b1)
{
if (Stopped) {
   Stopped = false;
   run = setInterval('aa(a1, b1)',25);
}
}

when i start it i got error - "a1 is undefined". even if i use try:

function blabla(a1, b1)
{
var a=a1;
var b=b1;
if (Stopped) {
   Stopped = false;
   run = setInterval('aa(a, b)',25);
}
}
the result is error...
0
 
LVL 2

Author Comment

by:djokov
ID: 6393712
... this time "a is undefined"?!?
0
 
LVL 14

Accepted Solution

by:
avner earned 200 total points
ID: 6393731
Cam you use this approach :

<SCRIPT LANGUAGE="JavaScript">
<!--
var a;
var b;
Stopped=true
blabla("66", "77")
function blabla(a1, b1)
{
a=a1;
b=b1;
if (Stopped) {
  Stopped = false;
  run = setInterval('aa(a, b)',250);
}
}

function aa(r,t){window.status="["+r+","+t+"]"}

//-->
</SCRIPT>
0
 
LVL 2

Author Comment

by:djokov
ID: 6393760
:-)) it works, but if you move
var a;
var b;
to the body of blabla you will get the error i told you...
ok, now is clear.
thanx
0
 
LVL 14

Expert Comment

by:avner
ID: 6393776
I guess you mixed between "local variables" and "global variables" .
But I guess it's all over now....:)
0
 
LVL 2

Author Comment

by:djokov
ID: 6393810
i didn't mix them... but when I have to do something in the last minute such things happens.. anyway.. ;-)

This script is used to rotate dynamicly created SVG 3D bar chart created only (!) with XSL with no extension function on huge xml data with terrible structure... can you image it!!! %-)

cheers
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question