Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to interrupt javascript code?

Posted on 2001-08-16
13
Medium Priority
?
2,139 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 800 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

705 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