Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2016
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with JS project.

    Hello. I'm a newbie at coding. What is the best way to run 2 functions where the results from both appear in the same modal window? I know that JavaScript is not multithread capable, but is there a jQuery or other workaround? Thanks for any help you can provide. Robert

  2. #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,774
    Thanks
    58
    Thanked 678 Times in 673 Posts
    you could look at web workers which absolutely provide multithreading capabilities to JavaScript - in fact that's what they were invented for
    https://developer.mozilla.org/en-US/...ng_web_workers

    WW's are gaining support but unreliable for older browsers (hello again, IE)

    jQuery is just JavaScript, written in a prettier way (depending on your perspective) - there are no magic bullets there.

    I suspect, though, that there may be a simpler solution to what you are trying to do. Would you like to provide some more detail?

  3. #3
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,918
    Thanks
    3
    Thanked 415 Times in 404 Posts
    I think we'd really have to know how you're handling the modal window, what the functions are, why you're calling them separately, etc, etc.

    There is a a sneaky trick you can use of setting a timeout of zero to queue each of them if you want them run after the current code finishes. You could then make a monitor 'process' that you queue each of them into that they then report themselves to when completed.

    A mini cooperative multitasking scheduler isn't TOO hard to implement.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  4. #4
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,918
    Thanks
    3
    Thanked 415 Times in 404 Posts
    Actually not that long ago I had a client who had this REALLY massive client-side scripting task that was taking so long that we had to break it into smaller chunks, otherwise browsers would throw up their "this process is taking too long" warning. Giving the browser even a fraction of a moment to breathe is all it took to avoid this -- a setTimeout(callback, 0); being all the time it needed. Even gave us a chance to update a progress bar on screen.

    Lemme... yeah, found it. Lemme gut out the stuff you wouldn't need (like the progress bar)... core of this shouldn't be too hard to understand.

    Code:
    // as a timeout handler and 'singleton' pattern it's safer not to use 'this'
    
    makeTask = function(callback) {
    	callback = {
    		callback : callback,
    		nextTask : null
    	}
    	if (makeTask.lastTask)
    		makeTask.lastTask = (makeTask.lastTask.nextTask = callback);
    	else {
    		makeTask.firstTask = makeTask.lastTask = callback;
    		setTimeout(makeTask.handler, 0);
    	}
    }
    
    /*
    	make event handler static so it's not proceessed as new code every
    	time we run 'makeTask'. I apply this as a property of makeTask as to
    	reduce the global function footprint.
    */
    
    makeTask.handler = function() {
    	makeTask.firstTask.callback();
    	if (makeTask.firstTask = makeTask.firstTask.nextTask)
    		setTimeout(makeTask.handler, 0);
    	else
    		makeTask.firstTask = makeTask.lastTask = null;
    }
    To use it you just:

    makeTask(yourCallbackFunction);

    Your callbacks can even enque other callbacks to be handled as they're all stored as a pointered list. (which was faster/cleaner than using an Array).

    For example:

    Code:
    function thirdTask() {
    	console.log('Third task');
    }
    
    makeTask(function() {
     console.log('First task');
     makeTask(thirdTask);
    });
    
    makeTask(function() {
    	console.log('Second task');
    });
    None of those functions will run until after primary script execution stops and the browser has a chance to clean up. The third task isn't added to the queue until the first task is run, letting the second task be slipped in since our pointered list is a FIFO buffer.

    Is that along the lines of what you need? This is a very basic/rough implementation of how cooperative multitasking works -- how early MacOS and Windows handled things before preemptive multitasking was possible/practical.

    Decent option if you need to support older browsers that have no clue what webWorkers are. Workers could also be used to optionally further enhance this as well as a means of supporting the old and the new.
    Last edited by deathshadow; Jul 14th, 2018 at 07:14 AM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •