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 ?

Recommended Answers

All 5 Replies

Member Avatar for stbuchok

Could you maybe write the sudo code for what you are talking about?

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(){

}
Member Avatar for stbuchok
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.

commented: Solved my problem +2

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 ?

Member Avatar for stbuchok

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

Sounds about right to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.