473,231 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Issue with function, need to use setTimeout

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
6 1722
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
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
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
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Stacey | last post by:
Hi, I'm hoping for a bit of advise-- I have a (relatively, from the point-of-view of this dilettante) complex script that attempts to preload certain images in order to trigger one of a series of...
4
by: coolsti | last post by:
I am aware of the setInterval and setTimeout functions for Javascript, but I can't seem to find an example that lets me do what I need to. I have a script that needs to wait until a certain...
1
by: CES | last post by:
All, I was wondering if someone could point me to a tutorial on creating & accessing functions from within a wrapper function. I've created a group of functions related to a timer event. All...
4
by: hsonny | last post by:
Hi there, I have an ASP page using the treeview control from mscomctl.ocx that was running just fine. We updated our browsers with this package ( details at...
6
by: Nico VanHaaster | last post by:
Hello all, I have run across an issue with IE 6.0+. I have a page that makes an XMLHttpRequest to the webserver to update a report on the page. The first time you hit the refresh report button...
2
by: WGW | last post by:
Hello all, I need another set of eyes cause it just isn't working, no matter how identical I make it. The initRotator function works when called by itself, but when adding another function, only the...
1
by: chaitanyadotcom | last post by:
As per my application i need to create tabs using iFrame dynamically. There are totally 4 buttons in my application where for each button i provide a link. Where in it will dynamically create a tab...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.