0

Question it title, how to do it ?

I need to stop a function with another function or I need to call a global function inner function with another global function so I could stop it that way. I know its possible but how ?

2
Contributors
5
Replies
9
Views
5 Years
Discussion Span
Last Post by stbuchok
0
function change(){
//changes slides in my div
}

function stop_change(){ //Pause would be even better
//Should stop the change() function and make the slideshow pause
}

This is my actual code at the moment:

function change(){

	/*Change these values according to your needs*/

	if(typeof(step) == "undefined"){
		newElem = document.createElement('img');
		linkPause = document.createElement('a');
		namePause = document.createTextNode("Pause");
		linkPause.appendChild(namePause);
		linkPause.setAttribute("onclick", "pause()");
		linkPause.style.position = "absolute";
		
		slideDiv = document.getElementById('slideshow'); //Name of the division that the slideshow will occur on
		slideDiv.appendChild(newElem);
		slideDiv.appendChild(linkPause);

		extention = "jpg";
		imgPath = "images/"
		numImg = 2; //How many images you want to add in slideshow. Slides start from 1 and are limited with this variable
	}

	((typeof(step) != "undefined" && step<=numImg) ? step = step : step = 1);
	newElem.src = imgPath + step + "." + extention;
	step++;
	setTimeout("change()", 2000);
}

function pause(){

}
1
var timer = null;

function change(){
 
	/*Change these values according to your needs*/
 
	if(typeof(step) == "undefined"){
		newElem = document.createElement('img');
		linkPause = document.createElement('a');
		namePause = document.createTextNode("Pause");
		linkPause.appendChild(namePause);
		linkPause.setAttribute("onclick", "pause()");
		linkPause.style.position = "absolute";
 
		slideDiv = document.getElementById('slideshow'); //Name of the division that the slideshow will occur on
		slideDiv.appendChild(newElem);
		slideDiv.appendChild(linkPause);
 
		extention = "jpg";
		imgPath = "images/"
		numImg = 2; //How many images you want to add in slideshow. Slides start from 1 and are limited with this variable
	}
 
	((typeof(step) != "undefined" && step<=numImg) ? step = step : step = 1);
	newElem.src = imgPath + step + "." + extention;
	step++;
	timer = setTimeout("change()", 2000);
}
 
function pause(){
   clearTimout(timer);
}

Basically create a variable that is going to hold the numeric value for your timer. Then when you pause, clearout the timer, essentially pausing it. If you need it to start again, call the change function.

Votes + Comments
Solved my problem
0

Thanks, it worked perfectly ! I just have one more question. Is there any way to resume the change() func from where it left off. I mean so it would resume with same picture it was paused with. Should I create a variable to hold the variable step data and then reuse it or is there any better way to do it ?

0

Should I create a variable to hold the variable step data and then reuse it

Sounds about right to me.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.