By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,453 Members | 3,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,453 IT Pros & Developers. It's quick & easy.

Issue with function, need to use setTimeout

P: n/a
Hey Guys,

I've stumped.

I created a function that does various things to select boxes.

Namely

Get All selected indexes,
populate array with these values
remove options
create options making sure selectedindexes are correct and other
selects dont have selected indexes as options.

Basically i needed to be able to have a situation where each select
didnt have the already selected options of other selects.

So if the possible options were A,B,C and one select had selected index
of A then the second select should only allow options B, C. Make sense
i hope?

Anyway, it all works.

But for some reason i need to add a setTimeout("function_name()", 1);
when calling the fuction, if i dont then i get an error. It seems as
if the function is running to fast for itself.

Can anyone shed some light as to why i needed to add the timeout?

Much appreciated.

Andy

Sep 15 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
daveyand wrote:
<snip>
Can anyone shed some light as to why i needed to add the timeout?
<snip>

Can anyone tell you why code they cannot see requires an apparently
unlikely action on your part? I doubt it. You may get lots of
speculation, which may or may not be relevant, but you are unlikely to
get the answer.

Richard.

Sep 15 '06 #2

P: n/a
Valid point.

Just wondered if someone had come across the same thing, where a
function that works perfectly when called the first time, messes up the
second.
Richard Cornford wrote:
daveyand wrote:
<snip>
Can anyone shed some light as to why i needed to add the timeout?
<snip>

Can anyone tell you why code they cannot see requires an apparently
unlikely action on your part? I doubt it. You may get lots of
speculation, which may or may not be relevant, but you are unlikely to
get the answer.

Richard.
Sep 15 '06 #3

P: n/a
daveyand wrote:
Valid point.

Just wondered if someone had come across the same thing, where a
function that works perfectly when called the first time, messes up the
second.
Yes, I did.
I made some code in the past that only ran the first time to my
disappontment. I also solved it. But what use is that to you?
It won't solve your problem.

Like Richard said, you most probably won't receive an answer without showing
us the relevant parts of the code.

Your question is a lot like: "Hi mechanic, why does my car not work anymore?
Last mile it drove just fine. What should I do?"

That mechanic really wants to know how the engine is looking and/or what you
did with the car before he can give any advise. :-/

Just post relevant code, preferably stripped down to the bare basic problem.

I am sure somebody can help you then.

Regards,
Erwin Moller

>
Richard Cornford wrote:
>daveyand wrote:
<snip>
Can anyone shed some light as to why i needed to add the timeout?
<snip>

Can anyone tell you why code they cannot see requires an apparently
unlikely action on your part? I doubt it. You may get lots of
speculation, which may or may not be relevant, but you are unlikely to
get the answer.

Richard.
Sep 15 '06 #4

P: n/a
daveyand wrote:
Valid point.
What point?
Just wondered if someone had come across the same thing, where a
function that works perfectly when called the first time, messes up the
second.
Yes, many. Now where did that get you?

Richard.

Sep 15 '06 #5

P: n/a
<html>
<head>
<script>

var array_discount = new Array("Re-Buying Discount|RE_BUY","Bulk Buy
Discount|BULK_BUY","Its Wednesday Discount|WED","Roger That
Discount|ROG_");

var array_discounts_already_selected = new Array();

var array_reference_dropdowns = new Array();

var int_discount_row_counter = 0;

var bool_dont_create_more_rows = 0;

/**
* get_all_selectedIndexes - Populates
array_discounts_already_selected with the selectedIndexes of the
current dropdown boxes
*
* @version 1.0 15/09/2006
* @author Andrew Davey (ad****@adviva.com)
*/
function get_all_selectedIndexes () {

for (n=0;n < array_discounts_already_selected.length;n++) {
array_discounts_already_selected[n] = 0;
}//for (n=0;n < array_discounts_already_selected.length;n++)

for(n=0;n<array_reference_dropdowns.length;n++) {

if(array_reference_dropdowns[n].options.length 0) {
var current_selected_index =
array_reference_dropdowns[n].selectedIndex;

/**
* This is the actual value in the DropDown that will be the index
in array_discounts.
* @variable integer current_selected_index_value
*/
var current_selected_index_value = array_reference_dropdowns[n][
current_selected_index ].value;

array_discounts_already_selected[current_selected_index_value] =
n+1;

}//if(array_reference_dropdowns[n].options.length 0)

}//for(n=0;n<array_reference_dropdowns.length;n++)

}//function get_all_selectedIndexes ()

/**
* create_discount_drop_down - Creates the dropdown select box on the
fly, adds it to whichever table cell is passed as parameters
*
* @params object object_table_cell
* @params integer int_discount_number
*
*/

function create_discount_drop_down
(object_table_cell,int_discount_number) {

var bool_first_run = 1;
var id = "discount_dd_" + int_discount_number;

/**
* The object that is used when creating the dropdown menu. It is
later added to an array.
* @variable object discount_drop_down_select
*/
var discount_drop_down_select;

//NOTE: array_discount is an array that is populated with PHP and
MYSQL in the calling page.

for(n=0;n<array_discount.length;n++) {

if(bool_first_run == 1) {
/**
* Create an object of a select, defining the ID and the NAME.
* @variable discount_drop_down_select
*/
discount_drop_down_select = document.createElement("select");
discount_drop_down_select.id = id;
discount_drop_down_select.name = id;

//append to the child of the table cell object passed.
object_table_cell.appendChild(discount_drop_down_s elect);

bool_first_run = 0;

}//if(bool_first_run == 1)

}//for(n=0;n<discount.length;n++)

array_reference_dropdowns[ array_reference_dropdowns.length++ ] =
discount_drop_down_select;

do_options_for_dropdowns();

// THIS IS SHIT!!!! //
// We have the timeout because IE seems to execute this far to
quickly. Fixes the issue, massive hack
// Call it again, because the newest dropdown upon creation has no
options, so it meant that the previous drop-
// -downs still had the values, due to no selectedIndex.
// NOTE: To see what i mean comment this line out and press (+) More
Discounts and view the dropdowns

setTimeout('do_options_for_dropdowns()', 1);
//do_options_for_dropdowns();
if(array_reference_dropdowns.length == ( array_discount.length ) ) {
bool_dont_create_more_rows = 1;

} else {
//we might have set this and want to unset it, due to a user
deleting a discount
bool_dont_create_more_rows = 0;

}//if(array_reference_dropdowns.length == ( array_discount.length ) )
}//function create_discount_drop_down
(object_table_cell,int_discount_number)
// -----------------------
/**
* do_options_for_dropdowns - This will loop through the discount
array and cretae all the options for each dropdown box. It will also
commpare against array_discounts_already_selected to determine what
options should be displayed
*
*
*/
function do_options_for_dropdowns() {

get_all_selectedIndexes();

// We first loop through the dropdowns created to get the value of
the selected indexes.
// We populate array_discounts_already_selected with these values,
but the value of the array is dropdown number+1.
// So something like this:
// array_discounts_already_selected[ index_of_discount_array ] =
Drop_down_number+1;
// example
// dd1.selectedIndex.value = 3;
// array_discounts_already_selected[3] = 2; Meaning that dropdown
1 is the dropdown that has seleced index of 3
// We add one because dropdown indexing starts from 0. And 0 is
undefined in JS.
// BUT only if the array is greater than 1. Otherwise there wont be
any options to loop :)
for(n=0;n<array_reference_dropdowns.length;n++) {

if(array_reference_dropdowns[n].options.length 0) {
//Remove all the options now, we re-create them below
for(z = (array_reference_dropdowns[n].options.length - 1); z >= 0 ;
z--) {
array_reference_dropdowns[n].options[z] = null;

}//for(x = 0; x < array_reference_dropdowns[n].options.length; x++)
{

}//if(array_reference_dropdowns[n].options.length 0)

}//for(n=0;n<array_reference_dropdowns.length;n++)

// We now create all the options for the DropDowns. Making sure we
keep the selected indexes and also not creating the options for
// already selected indexes.
for(n=0;n<array_reference_dropdowns.length;n++) {

var selected_index_counter = 0;

// Loop through the array_discount, this is defined on the previous
page. Its an array populated by PHP and MySQL
for(i=0;i<array_discount.length;i++) {

/**
* The Discount name of the current index in the array_discount
* @variable string discount_name
*/

var discount_name = get_discount_name(i);

// Make sure that array_discounts_already_selected[i] isnt set. If
it is AND is the value of N then we set that to be selectedIndex.
// If it is set and ISNT the value of N then we dont create one.
if(!array_discounts_already_selected[i]) {

/**
* Create an option that is added to the select created above.
Loops through discount array
* @variable option
*/

var option = document.createElement('option');
option.value = i;
option.appendChild(document.createTextNode(discoun t_name));
array_reference_dropdowns[n].appendChild(option);

selected_index_counter++;

} else if(array_discounts_already_selected[i] == n+1 ) {
/**
* Create an option that is added to the select created above.
Loops through discount array
* @variable option
*/

var option = document.createElement('option');
option.value = i;
option.appendChild(document.createTextNode(discoun t_name));
array_reference_dropdowns[n].appendChild(option);

array_reference_dropdowns[n].selectedIndex =
selected_index_counter;

}//if(!array_discounts_already_selected[i])

}//for(i=0;i<array_discount.length;i++)

}//for(n=0;n<array_reference_dropdowns.length;n++)

}//function do_options_for_dropdowns
//-------------------------------------

/**
* add_new_discount_row - Create a new DOM row within the discount
table on the page. Looks for a parent_id and then creates a child row
*
* @params string parent_id
*
*/
function add_new_discount_row(parent_id) {
//< o_0 >//

if(!bool_dont_create_more_rows) {
var table = document.getElementById(parent_id);
var row_list = table.getElementsByTagName("TR");
var int_which_row;
//Loop through the rows and see which has the id:
add_discounts_above_me this is the one with the link.
//We want to set this as the index-1. So that the new discounts
come above it.

for(n=0;n<row_list.length;n++) {

if(row_list[n].id == "add_discounts_above_me") {

int_which_row = n;
break;
}
}//for(n=0;n<row_list.length;n++)

//We need to do this because for some reason it wont create the row
in the right area. It could be scope, but doesnt look like it.
//Its fixed anyway
var int_number_of_rows = int_which_row;

var row = table.insertRow(int_number_of_rows);

row.id = "discount_row_" + int_discount_row_counter;

var discount_dropdown_cell = row.insertCell(0);
discount_dropdown_cell.id = row.id + "_cell_0"; //Used when
swapping the stuff around

create_discount_drop_down(discount_dropdown_cell,
int_discount_row_counter);
int_discount_row_counter++;

}//if(!bool_dont_create_more_rows)

}//function add_new_discount_row (parent_object)

/**
* get_discount_name - Get the name of the discount, used in a number
of places
*
* @version 1.0 13/09/2006
* @author Andrew Davey (ad****@adviva.com)
* @params object int_discount_array_index
* @returns string discount_name
*/
function get_discount_name (int_discount_array_index) {
return array_discount[int_discount_array_index].split("|")[0];
}//function get_discount_name (int_discount_array_index)

/// PARENT_ID in the above function should be the id of a TR. (Or what
ever in a table)
</script>

</head>

<body>

<table id="blah">

<tr id="Parent_ID">
</tr>

<tr>
<td><a href="#" onclick="add_new_discount_row('blah');">Add New
Row</a></td>
</tr>

</table>

-------------------

Copy and paste the above and add it to a new HTML page.

You'll see if you click add new row a new row and select is created.
You should notice that each dropdown doesnt display any of the
selectedIndexes already selected.

Now,

if you comment out line 97 and uncomment 98 you will notice a JS error.
It seems that the fucntion runs to fast. Or at least that's what i
think is happenening, hence why i added the setTimeout.

CHeers for any help.

Sep 15 '06 #6

P: n/a
JRS: In article <11**********************@m73g2000cwd.googlegroups .com>,
dated Fri, 15 Sep 2006 07:45:34 remote, seen in
news:comp.lang.javascript, daveyand <an**********@gmail.composted :
>
Copy and paste the above and add it to a new HTML page.
Fails, because you have allowed your posting agent to line-wrap the
source.

Please do not use tabs to indent code in News : any reasonable editor
will be able to globally replace tabs by space pairs.

Your code seems to have one more { than } though it is possible that you
have some in strings or comment.

You should validate your HTML.

See various parts of
<URL:http://www.merlyn.demon.co.uk/js-index.htm>
<URL:http://www.merlyn.demon.co.uk/js-other.htm etc.

It's a good idea to read the newsgroup and its FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.