Current location: Hot Scripts Forums » General Web Coding » JavaScript » re-set var from function


re-set var from function

Reply
  #1 (permalink)  
Old 06-11-09, 07:28 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
re-set var from function

Hi there..

I want to use 2 varibles in my javascript wich I set in this piece of code:
Code:
<script language="JavaScript">
var prev=1;
var next=2;
</script>
in the following function I want to re-set these variables..
But how can I use the new values in my site?..
The alert is to show the values in the function are right.


Code:
function slideFolio(col){
	var prev = col-1;
	var next = col+1;	
	alert(prev + ' - ' + next);
}
And this is part of my site where I use these 2 snippets

Code:
<div id="button_left" onclick="slideFolio(prev);"></div>
<div id="mask"></div>
<div id="button_right" onclick="slideFolio(next);"></div>
Who can help me out here....
Reply With Quote
  #2 (permalink)  
Old 06-11-09, 09:03 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Remove the vars inside the function.
Reply With Quote
  #3 (permalink)  
Old 06-11-09, 09:50 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
................................
__________________
Jerry Broughton

Last edited by job0107; 06-11-09 at 09:56 AM.
Reply With Quote
  #4 (permalink)  
Old 06-11-09, 10:27 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by jonnekke View Post
Hi there..

I want to use 2 varibles in my javascript wich I set in this piece of code:
Code:
<script language="JavaScript">
var prev=1;
var next=2;
</script>
in the following function I want to re-set these variables..
But how can I use the new values in my site?..
The alert is to show the values in the function are right.


Code:
function slideFolio(col){
    var prev = col-1;
    var next = col+1;    
    alert(prev + ' - ' + next);
}
And this is part of my site where I use these 2 snippets

Code:
<div id="button_left" onclick="slideFolio(prev);"></div>
<div id="mask"></div>
<div id="button_right" onclick="slideFolio(next);"></div>
Who can help me out here....
I am trying to understand what your reference to 'next' and 'prev' mean.
You are changing two different variables ("next' and 'prev').
Eventually 'prev' will become a negative number.
And 'next' can go on to infinity.

How you are using these two variables together escapes me.

Logic would tend to indicate that 'next' and 'prev', increment or decrement a single variable that is probably used as an object pointer.

Say for instance you are trying to create a Slide Show of images or documents or pages.
And you want to change them by clicking a 'next' or 'prev' button.

I would do something like this:
This script uses an array to hold image file names.
It could easily be adopted to use document file names.
Or different page files.
HTML Code:
<script language="JavaScript">
// Replace the image file names in this array with your image file names.//
var slideImages = new Array("image1.jpg","image2.jpg","image3.jpg","image4.jpg","image5.jpg","image6.jpg","image7.jpg","image8.jpg","image9.jpg","image10.jpg");

var current = 0; // Set to first slideImages value.
var slide; // Used by setInterval().

function slideFolio(col){
    if(col == "prev"){current-=1;}  // Decerement the current slide number.
    if(col == "next"){current+=1;}  // Increment the current slide number.
    if(current < 0){current = slideImages.length-1;} // This rolls the slide number from first to last.
    if(current == slideImages.length){current = 0;}  // This rolls the slide number from last to first.
    var obj = document.getElementById("slide_image"); // Get the 'image object'.
    obj.src = slideImages[current]; // Assign the slideImages value to the 'image object' src property.
    obj.alt = slideImages[current]; // Assign the slideImages value to the 'image object' alt property.
}

function startShow()
{
 stopShow();
 current = -1;
 slide = setInterval("slideFolio('next')", 2000); // This interval is set for 2 seconds between images (1000 = 1 second). //
 }

function stopShow()
{
 if(slide){clearInterval(slide);} // This clears the interval if one is started and stops the slide show.
 }
</script>
<table>
 <tr><td colspan="2" align="center"><img id="slide_image" src="image1.jpg" alt="image1.jpg" /></td></tr>
 <tr><td align="right"><button id="button_left" onclick="slideFolio('prev');">Prev</button></td><td><button id="button_right" onclick="slideFolio('next');">Next</button></td></tr>
 <tr><td><button id="button_right" onclick="startShow();">Start Slide Show</button></td><td><button id="button_right" onclick="stopShow();">Stop Slide Show</button></td></tr>
</table>
[html]
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to modify flash so it pulls php url every 15 seconds method Flash & ActionScript 7 03-05-07 06:39 AM
RSS Using RSS2Html Script VKX PHP 1 10-16-06 05:27 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
Scrollbar!!!!!! LiLSweetie HTML/XHTML/XML 8 07-22-04 05:01 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM


All times are GMT -5. The time now is 01:52 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.