473,657 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("fun ction_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 1751
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_B UY","Its Wednesday Discount|WED"," Roger That
Discount|ROG_") ;

var array_discounts _already_select ed = new Array();

var array_reference _dropdowns = new Array();

var int_discount_ro w_counter = 0;

var bool_dont_creat e_more_rows = 0;

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

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

for(n=0;n<array _reference_drop downs.length;n+ +) {

if(array_refere nce_dropdowns[n].options.length 0) {
var current_selecte d_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_selecte d_index_value
*/
var current_selecte d_index_value = array_reference _dropdowns[n][
current_selecte d_index ].value;

array_discounts _already_select ed[current_selecte d_index_value] =
n+1;

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

}//for(n=0;n<array _reference_drop downs.length;n+ +)

}//function get_all_selecte dIndexes ()

/**
* 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_ce ll
* @params integer int_discount_nu mber
*
*/

function create_discount _drop_down
(object_table_c ell,int_discoun t_number) {

var bool_first_run = 1;
var id = "discount_d d_" + int_discount_nu mber;

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

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

for(n=0;n<array _discount.lengt h;n++) {

if(bool_first_r un == 1) {
/**
* Create an object of a select, defining the ID and the NAME.
* @variable discount_drop_d own_select
*/
discount_drop_d own_select = document.create Element("select ");
discount_drop_d own_select.id = id;
discount_drop_d own_select.name = id;

//append to the child of the table cell object passed.
object_table_ce ll.appendChild( discount_drop_d own_select);

bool_first_run = 0;

}//if(bool_first_r un == 1)

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

array_reference _dropdowns[ array_reference _dropdowns.leng th++ ] =
discount_drop_d own_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_dro pdowns()', 1);
//do_options_for_ dropdowns();
if(array_refere nce_dropdowns.l ength == ( array_discount. length ) ) {
bool_dont_creat e_more_rows = 1;

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

}//if(array_refere nce_dropdowns.l ength == ( array_discount. length ) )
}//function create_discount _drop_down
(object_table_c ell,int_discoun t_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_select ed to determine what
options should be displayed
*
*
*/
function do_options_for_ dropdowns() {

get_all_selecte dIndexes();

// We first loop through the dropdowns created to get the value of
the selected indexes.
// We populate array_discounts _already_select ed with these values,
but the value of the array is dropdown number+1.
// So something like this:
// array_discounts _already_select ed[ index_of_discou nt_array ] =
Drop_down_numbe r+1;
// example
// dd1.selectedInd ex.value = 3;
// array_discounts _already_select ed[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_drop downs.length;n+ +) {

if(array_refere nce_dropdowns[n].options.length 0) {
//Remove all the options now, we re-create them below
for(z = (array_referenc e_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_refere nce_dropdowns[n].options.length 0)

}//for(n=0;n<array _reference_drop downs.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_drop downs.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.lengt h;i++) {

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

var discount_name = get_discount_na me(i);

// Make sure that array_discounts _already_select ed[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_disco unts_already_se lected[i]) {

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

var option = document.create Element('option ');
option.value = i;
option.appendCh ild(document.cr eateTextNode(di scount_name));
array_reference _dropdowns[n].appendChild(op tion);

selected_index_ counter++;

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

var option = document.create Element('option ');
option.value = i;
option.appendCh ild(document.cr eateTextNode(di scount_name));
array_reference _dropdowns[n].appendChild(op tion);

array_reference _dropdowns[n].selectedIndex =
selected_index_ counter;

}//if(!array_disco unts_already_se lected[i])

}//for(i=0;i<array _discount.lengt h;i++)

}//for(n=0;n<array _reference_drop downs.length;n+ +)

}//function do_options_for_ dropdowns
//-------------------------------------

/**
* add_new_discoun t_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_discoun t_row(parent_id ) {
//< o_0 >//

if(!bool_dont_c reate_more_rows ) {
var table = document.getEle mentById(parent _id);
var row_list = table.getElemen tsByTagName("TR ");
var int_which_row;
//Loop through the rows and see which has the id:
add_discounts_a bove_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_l ist.length;n++) {

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

int_which_row = n;
break;
}
}//for(n=0;n<row_l ist.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_r ows = int_which_row;

var row = table.insertRow (int_number_of_ rows);

row.id = "discount_r ow_" + int_discount_ro w_counter;

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

create_discount _drop_down(disc ount_dropdown_c ell,
int_discount_ro w_counter);
int_discount_ro w_counter++;

}//if(!bool_dont_c reate_more_rows )

}//function add_new_discoun t_row (parent_object)

/**
* get_discount_na me - 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_ar ray_index
* @returns string discount_name
*/
function get_discount_na me (int_discount_a rray_index) {
return array_discount[int_discount_ar ray_index].split("|")[0];
}//function get_discount_na me (int_discount_a rray_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_ne w_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************ **********@m73g 2000cwd.googleg roups.com>,
dated Fri, 15 Sep 2006 07:45:34 remote, seen in
news:comp.lang. javascript, daveyand <an**********@g mail.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.demo n.co.uk/js-index.htm>
<URL:http://www.merlyn.demo n.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.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
11430
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 suggestion? If I call the function like this: wait(500); it should wait 500ms right? function wait(time) { while(1){ setTimeout("break;",time); } }
3
14928
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
1
3377
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 six slideshows (rather than try to articulate, have a look here): http://www.slack.net/~stacey/stestbed/homejs.jsp Clicking any of the six blocks of text along the sides will begin a loop of the corresponding slideshow.
4
9505
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 condition is met. In this case, the condition is that an iframe has been reloaded completely. I do this by examining a hidden document variable in the iframe, and when it has changed from "" to "1" is the signal for me that the iframe is loaded. (if...
1
2523
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 works well until I try to enclose these functions into a class like structure ie: function dTimer(div){ //Insert the Javascript Code Below startTimer(); }
4
4771
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 http://support.microsoft.com/kb/912945/en-us) so I get "Unhandled exception at 0x275888cf in IEXPLORE.EXE: 0xC0000005: Access violation writing location 0x0000000b." The other mscomctl.ocx's controls (ListView, MonthView, ImageList...)
6
2531
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 the data is refreshed, however if you try to refresh the report the onreadystatechage does not seem to fire. I have tested this by placing an alert in the verifyID2 & verifyID3 function to track where the XMLHttpRequest currently is. Now when...
2
6154
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 "other function" works, not the initRotator function. Here's some examples of what I've tried 1st Example using the 2 functions on body tag: <head> <script src="css/dw_rotator.js" type="text/javascript"></script> <script...
1
5794
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 inside the iFrame. For example.. first button contains google.com second button contains yahoo.com 3rd button contains hotmail.com if i click on first button i.e google.com , tab is created and link to google.com is generated as a tab in...
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1971
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.