How to get sub-javascript to finish before executing next?

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
daxam
Posts: 13
Joined: Wed Mar 28, 2018 6:49 am

How to get sub-javascript to finish before executing next?

Post by daxam » Wed May 02, 2018 2:07 pm

Config Info
Firefox 56.0.2
imacros 8.9.7
windows 7 Pro


Hi,
I have a simple main javascript as shown below. The intention is to run the info-scraping.js and then wait for a finite time before it runs again. However, I found that the codes below will run info-scraping.js then followed by the "WAIT SECONDS" before info-scraping.js finished running. How do I code in a way that the line "WAIT SECONDS" will only run after info-scraping.js is completely done? Sorry, I've been googling and searching in the forum, couldn't find any answers. Appreciate your help. Thanks.

for (i=1; i<=10; i++){
iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");
iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
Last edited by daxam on Wed May 02, 2018 3:50 pm, edited 1 time in total.
chivracq
Posts: 8693
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to get sub-javascript to finish before executing nex

Post by chivracq » Wed May 02, 2018 3:00 pm

daxam wrote:Config Info
Firefox 56.0.2
imacros 8.9.7
windows 7 Pro


Hi,
I have a simple main javascript as shown below. The intention is to run the info-scraping.js and then wait for a finite time before it runs again. However, I found that the codes below will run info-scraping.js then followed by the "WAIT SECONDS" before info-scraping.js finished running. How do I code in a way that the line "WAIT SECONDS" will only run after info-scraping.js is completely done? Sorry, I've been googling and searching in the forum, couldn't find any answers. Appreciate your help. Thanks.

Code: Select all

for (i=1; i<=10; i++){
	iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");	
	iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
Your Thread Title could use a Question Mark btw, you are not sharing a 'How-To'... :wink:

To answer your Qt, even if I don't use '.js' Scripts myself but there are a few similar Threads on the Forum I think as I've already seen the Qt..., and the Sol I think is simply to declare the Return Code for the (first) 'iimPlay()' Statement... You don't even need to use/check it, simply declare it like:

Code: Select all

for (i=1; i<=10; i++){
	iRet=iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");	
	iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
daxam
Posts: 13
Joined: Wed Mar 28, 2018 6:49 am

Re: How to get sub-javascript to finish before executing nex

Post by daxam » Wed May 02, 2018 3:47 pm

chivracq wrote:
daxam wrote:Config Info
Firefox 56.0.2
imacros 8.9.7
windows 7 Pro


Hi,
I have a simple main javascript as shown below. The intention is to run the info-scraping.js and then wait for a finite time before it runs again. However, I found that the codes below will run info-scraping.js then followed by the "WAIT SECONDS" before info-scraping.js finished running. How do I code in a way that the line "WAIT SECONDS" will only run after info-scraping.js is completely done? Sorry, I've been googling and searching in the forum, couldn't find any answers. Appreciate your help. Thanks.

Code: Select all

for (i=1; i<=10; i++){
	iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");	
	iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
Your Thread Title could use a Question Mark btw, you are not sharing a 'How-To'... :wink:

Ya, sorry for the confusion, I missed the Question mark. :oops: Will make sure I put a question mark next time... :)

To answer your Qt, even if I don't use '.js' Scripts myself but there are a few similar Threads on the Forum I think as I've already seen the Qt..., and the Sol I think is simply to declare the Return Code for the (first) 'iimPlay()' Statement... You don't even need to use/check it, simply declare it like:

Code: Select all

for (i=1; i<=10; i++){
	iRet=iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");	
	iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
Thanks for helping, chivracq. I tried. Seems like the "WAIT SECONDS" line still proceed without waiting for the info-scraping.js to finish". See screenshot below. The 499.80 seconds shown is the "WAIT SECONDS" line code. Anything that I did wrong?
Java_Status.PNG
Java_Status.PNG (5.43 KiB) Viewed 2472 times
chivracq
Posts: 8693
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to get sub-javascript to finish before executing nex

Post by chivracq » Wed May 02, 2018 7:02 pm

daxam wrote:Thanks for helping, chivracq. I tried. Seems like the "WAIT SECONDS" line still proceed without waiting for the info-scraping.js to finish". See screenshot below. The 499.80 seconds shown is the "WAIT SECONDS" line code. Anything that I did wrong?
Java_Status.PNG
Ouf..., you don't "really" need to quote yourself again, that doesn't really help for "readability" of the Thread...

Hum, OK, strange... :?

I guess by calling a '.js' Script from a '.js' Script that iMacros loses "track" of which Script is being played (and is controlling iMacros) at that moment, this is not very "good Practice" anyway, I would think... You should have only one "main" '.js' Script that controls the whole Workflow of your Automation and that can call '.iim' Sub-Scripts if you want, but not another '.js' Script that will "think" when it's running that it is "the big Master of the Game" at that moment...

You can try otherwise adding some more "precise" conditional Control on the RetCode before launching the 'WAIT', and adding some 'alert()' just before the 'WAIT' to debug/check if the first '.js' has indeed finished running or not... But I guess once an 'iimPlay()' Command inside the '.js' Sub-Script has finished running, then iMacros already has one RetCode in Memory and that won't help anyway...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
daxam
Posts: 13
Joined: Wed Mar 28, 2018 6:49 am

Re: How to get sub-javascript to finish before executing nex

Post by daxam » Thu May 03, 2018 11:59 am

I'm not really experienced in forum posting, that's why I quoted myself again... :oops: Will take note in future..

I think you are right about "the big Master of the Game" point. iMacros got confused. I tried adding an alert() before launching the 'WAIT', it just pops up and continues without even me clicking on the "OK" button.

Next try, I added the conditional control on the RetCode shown below as suggested, the "WAIT" is not launched now, but here's comes the interesting part, I added a iimDisplay() to display the RetCode, the value is "-1001". Shouldn't the return code be either "0" or "1" only? I'm a bit confused now

for (i=1; i<=10; i++){
iRet=iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");
if (iRet == 0) {
iimPlay("CODE:WAIT SECONDS={{WAIT}}");
}
}
RetCode.PNG
RetCode.PNG (4.8 KiB) Viewed 2462 times
chivracq
Posts: 8693
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to get sub-javascript to finish before executing nex

Post by chivracq » Thu May 03, 2018 1:44 pm

daxam wrote:I'm not really experienced in forum posting, that's why I quoted myself again... :oops: Will take note in future..

I think you are right about "the big Master of the Game" point. iMacros got confused. I tried adding an alert() before launching the 'WAIT', it just pops up and continues without even me clicking on the "OK" button.

Next try, I added the conditional control on the RetCode shown below as suggested, the "WAIT" is not launched now, but here's comes the interesting part, I added a iimDisplay() to display the RetCode, the value is "-1001". Shouldn't the return code be either "0" or "1" only? I'm a bit confused now

Code: Select all

for (i=1; i<=10; i++){
	iRet=iimPlay("CODE:URL GOTO=imacros://run/?m=info-scraping.js");
	if (iRet == 0) { 
	iimPlay("CODE:WAIT SECONDS={{WAIT}}");
	}
}
RetCode.PNG
Hum..., "funny" about the JS 'alert()' being automatically dismissed..., could actually be an "interesting" Behaviour, ah-ah...! :twisted:

Euh..., RetCodes, there is no "0", only "1" if Success, and a whole bunch of Negative Values in case of any (Runtime) Errors, see the full List in the Wiki: Error and Return Codes
=> "-1001" = "FF: Unknown Error", ah-ah...!

You check in your Script on "if(iRet==0)", this will never happen then, as "0" is no valid RetCode...

But tja, like I said, calling a '.js' from a '.js' is no Good Practice, that's cruising for unexpected Behaviour, I'm actually nearly "surprised" iMacros is able to get back to your main '.js' at some point... When calling an '.iim' from an '.iim', when the 2nd '.iim' starts, it doesn't know anything at all from the 1st one and there is no way that once finished running it will give the Handle back to the 1st one.
You should simply have ONE single main '.js' Script containing all the Logic of your Automation... :idea:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
daxam
Posts: 13
Joined: Wed Mar 28, 2018 6:49 am

Re: How to get sub-javascript to finish before executing nex

Post by daxam » Thu May 03, 2018 3:17 pm

Ya, it's an "interesting" behaviour indeed seeing the alert() dismissed automatically.... :shock:

Thanks for clarification on the RetCode and the reference on the Error and Return Codes, this is going to be really helpful for me in future during debugging ... :D

Ya, I think I just have to re-write my codes then to have one single main '.js'. What a bummer! But guess you can't get everything in life sometimes... Haha...

Thanks so much for the help, chivracq. Really appreciate it.
chivracq
Posts: 8693
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: How to get sub-javascript to finish before executing nex

Post by chivracq » Thu May 03, 2018 3:57 pm

daxam wrote:Ya, it's an "interesting" behaviour indeed seeing the alert() dismissed automatically.... :shock:

Thanks for clarification on the RetCode and the reference on the Error and Return Codes, this is going to be really helpful for me in future during debugging ... :D

Ya, I think I just have to re-write my codes then to have one single main '.js'. What a bummer! But guess you can't get everything in life sometimes... Haha...

Thanks so much for the help, chivracq. Really appreciate it.
Yeah, well, I think your Sub-'.js' Script should become the "main" '.js' Script, it already contains 90% of your Logic, I reckon, and you simply need to add the Looping and the 'WAIT' there... (And the 'iimDisplay()' as well, that's "Good Practice", I miss that Functionality in pure '.iim'..., even if I have a few "Ways" to kind of emulate a bit that Functionality, but they are all a bit cumbersome and not as easy to use like the 'iimDisplay()' Command...)

And even if you already have some Looping in that Sub-'.js' Script, you can have as many Levels of Nested Looping as you want, even if 2 is usually the most you need...
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
daxam
Posts: 13
Joined: Wed Mar 28, 2018 6:49 am

Re: How to get sub-javascript to finish before executing nex

Post by daxam » Fri May 04, 2018 5:58 am

Agree with your point. I'm going to turn my sub-.'js' to become the main '.js' script. Guess that's the way to go from here. A bit unwillingly, but got to do it :lol:

Ya, the iimDisplay() is really helpful. Conditional control is easier also in '.js', maybe in my perspective. Thanks again for the guidance and sharing, chivracq.
ravi_npatty
Posts: 22
Joined: Sat May 07, 2011 6:12 am

Re: How to get sub-javascript to finish before executing nex

Post by ravi_npatty » Sun May 06, 2018 4:59 pm

Since you are with v8.9.7, you can try javascript include https://forum.imacros.net/viewtopic.php ... 968#p66951

You can save all your common functions in one .js file and include them on the fly on other scripts. You can use all the methods defined in that included file.
Post Reply