Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2186
  • Last Modified:

How to interrupt javascript code?

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
djokov
Asked:
djokov
  • 7
  • 6
1 Solution
 
avnerCommented:
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
 
avnerCommented:
you can also do :

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

and then when you want to stop the repeating method :

clearInterval(moshe)


0
 
djokovAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
avnerCommented:
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
 
djokovAuthor Commented:
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
 
avnerCommented:
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
 
avnerCommented:
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
 
djokovAuthor Commented:
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
 
djokovAuthor Commented:
... this time "a is undefined"?!?
0
 
avnerCommented:
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
 
djokovAuthor Commented:
:-)) 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
 
avnerCommented:
I guess you mixed between "local variables" and "global variables" .
But I guess it's all over now....:)
0
 
djokovAuthor Commented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now