Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Stall js function until sub-function returns user input?
Old 06-09-2009, 06:55 AM Stall js function until sub-function returns user input?
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
I have a javascript requirement which has me stumped, has anybody come across something like this before? i basically need a pause button pending further instruction.

Code:
return showPeople();

function showPeople(){
	//display an input box asking user to write a name and click submit...
	//generate a box full of matches to the user input...
//let the user click on one of the options, and return the relevant userid back to show people...
}

the problem with the above code is that it always returns undefined as the showPeople() function thinks its finished as soon as the input field has been generated, i need to pause the function until such a time as the user has chosen an option from the matches generated after they click the search button, then return from show people according to the id of the box they click.

Im intending this showPeople() function to be used from several different functions, so running a separate function from onclick of the selected id box is not practicle as I wouldn’t know which function to return the user to.


Thanks for looking

Mark m
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-09-2009, 12:35 PM Re: Stall js function until sub-function returns user input?
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Unfortunately, javascript doesn't come with pause, sleep, or wait.
Your best best is to create a state machine, and add an event on the checkbox.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 06-09-2009, 02:24 PM Re: Stall js function until sub-function returns user input?
Extreme Talker

Posts: 246
Trades: 0
you could use setInterval and then use a flag to break out of the loop when everything is ready.

The other thing is that a function should only ever do one thing (sometimes more.. but rule of thumb is one), you are making it the whole program.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 06-09-2009, 05:18 PM Re: Stall js function until sub-function returns user input?
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Quote:
Originally Posted by stbuchok View Post
The other thing is that a function should only ever do one thing (sometimes more.. but rule of thumb is one), you are making it the whole program.
Bonus points for mentioning SRP.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 09:28 AM Re: Stall js function until sub-function returns user input?
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
thanks for the feedback guys, i agree with th srp thing, but i also agree with keeping code to a minimum where possible, and i cant think through what im trying to do without making some very messy scripts.

the srp mentioned above refers to one particular function, what im trying to achieve would require atleast 3 functions to complete, which is fine, but the initial call function needs to return the result of the sub functions, and its becoing apparent that this isnt possible to do when user input is required inside one of the sub functions.

i think im going to make a function which will display items for the user to select, and each time i call it, il send a variable to it with the name of the function which will be called onclick when the user makes their selection like so

Code:
function showPeople(returnTo,firstId)
{
var str="<a href=\"#\" onclick=\""+returnTo+"("+id1+")\">".id1."</a>
   str+="<a href=\"#\" onclick=\""+returnTo+"("+id2+")\">".id2."</a>
}
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 06-10-2009, 11:56 AM Re: Stall js function until sub-function returns user input?
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
I think we're going to need a little more detail of what you're trying to accomplish.

Do you have a test page somewhere?
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Stall js function until sub-function returns user input?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29302 seconds with 12 queries