Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-11-26 07:17:55
Point:500 Replies:13 POST_ID:828538USER_ID:11059
Topic:
JavaScript;;
I got one array in javascript question. I would like to make iframe test to change
it src location for each 5 seconds to different websites. But it is failed.
I stored all need website name to array and make a loop to play/fire the website
But it always report undefine link if I active alert(link) at function play(link){
//alert(link);
document.getElementById("topiframe").src=link;
}
So now try to make a test at window.setTimeout(function() {play(string[5]);} , 5*1000*c);
to replace i varaible in string[] to be 5 , it works. the string[5] website is shown in iframe
So I suspect I define function(){playstring[])} in windowsetTimeout might be sytnax
issue, IS it right? Could you help to download the code and test it at your side?
Pleae advise
Duncan
it src location for each 5 seconds to different websites. But it is failed.
I stored all need website name to array and make a loop to play/fire the website
But it always report undefine link if I active alert(link) at function play(link){
//alert(link);
document.getElementById("topiframe").src=link;
}
So now try to make a test at window.setTimeout(function() {play(string[5]);} , 5*1000*c);
to replace i varaible in string[] to be 5 , it works. the string[5] website is shown in iframe
So I suspect I define function(){playstring[])} in windowsetTimeout might be sytnax
issue, IS it right? Could you help to download the code and test it at your side?
Pleae advise
Duncan
<html><head><script type="text/javascript">playstack();function play(link){//alert(link); document.getElementById("topiframe").src=link;}function playstack() {var string=new Array();string[1]='http://www.yahoo.com/';string[2]='http://www.face.com/';string[3]='http://www.youtube.com/';string[4]='http://hk.motorola.com/r/us/';string[5]='http://www.google.com/';var i=1;var s=1;var c=1;while (i < 6){alert(string[i]);window.setTimeout(function() {play(string[i]);} , 5*1000*c);c=c+1;i=i+1;//if (s=6) {s=1;}}}</script></head><body><div class="i6" > <IFRAME name='topiframe' noresize id='topiframe' width=500 height=500 SCROLLING=no border=0 frameborder=0 src='http://www.google.com/index.php' ></IFRAME> </div> </body> </html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:
Attachment:array.html
Author: duncanb7 replied at 2024-11-28 18:11:03
THanks for all of you to reply this thread
Duncan
Assisted Solution
Expert: gnoon replied at 2024-11-28 17:49:44
125 points EXCELLENT
I learned from existing code and search reference from the Internet for sometime. hehe
The syntax I said it's called "Closure". You may find its document from here.
Well, the best way to learn is try it, I think.
The syntax I said it's called "Closure". You may find its document from here.
Well, the best way to learn is try it, I think.
Author: duncanb7 replied at 2024-11-28 12:16:09
How you know that ? do you have document about this Javascript synatx like ?:(trinary)
Sometimes, I read others' javascript code is hard to follow because of a lot of non-official
syntax or his-own syntax like Jquery
Sometimes, I read others' javascript code is hard to follow because of a lot of non-official
syntax or his-own syntax like Jquery
Assisted Solution
Expert: gnoon replied at 2024-11-26 19:09:31
125 points EXCELLENT
duncanb7,
JavaScript allows us to call a anonymous function (define & call at the same time) by following syntax
(***)(???);
*** is your function, arguments can be defined
??? is parameters matched the arguments
For example
JavaScript allows us to call a anonymous function (define & call at the same time) by following syntax
(***)(???);
*** is your function, arguments can be defined
??? is parameters matched the arguments
For example
*** = function(i){window.setTimeout(function() {play(string[i]);} , 5*1000*c);}??? = i 1:2:
So
is possible.
Expert: COBOLdinosaur replied at 2024-11-26 09:20:34
I'm working on 12 question on three different sites. If you don't have the time to test suggest to solve your own problem, and you can't provide simple information like what the error is, then I guess I should cut back to 11 questions.
Cd&
Cd&
Author: duncanb7 replied at 2024-11-26 09:07:17
Dear Cd,
error at 15 cnt=(cnt!<Ustring.length) ? 0 : cnt+1;
Did you try you code first before post
error at 15 cnt=(cnt!<Ustring.length) ? 0 : cnt+1;
Did you try you code first before post
<html><head><script type="text/javascript">var Ustring=new Array();Ustring[0]='http://www.yahoo.com/';Ustring[1]='http://www.facebook.com/';Ustring[2]='http://www.youtube.com/';Ustring[3]='http://www.ibm.com/';Ustring[4]='http://www.google.com/';cnt=0;function play(link){ document.getElementById("topiframe").src=link; cnt=(cnt!<Ustring.length) ? 0 : cnt+1; ST=setTimeout('play(Ustring[cnt])', 5*1000);}function playstack() {play(ustring[0]);}onload=playstack;</script></head><body> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:
Accepted Solution
Author: duncanb7 replied at 2024-11-26 09:01:15
Dear qnoon,
you are right after I re-test your code, all websites are shown in iframe
for every 5 second.
It shouned function(i){} you want to force to pass i varaible into play() in
setTimeout function, Right ?
But why you can (i) at th end ? For what ?
Please advise
you are right after I re-test your code, all websites are shown in iframe
for every 5 second.
It shouned function(i){} you want to force to pass i varaible into play() in
setTimeout function, Right ?
But why you can (i) at th end ? For what ?
Please advise
html><head><script type="text/javascript">playstack();function play(link){//alert(link); document.getElementById("topiframe").src=link;}function playstack() {var i=1;var s=1;var c=1;while (i < 6){var string=new Array();string[1]='http://www.yahoo.com';string[2]='http://www.facebook.com';string[3]='http://www.google.com';string[4]='http://www.facebook.com';string[5]='http://www.appledaily.com';//alert(string[i]);//window.setTimeout(function() {play(string[i]);} , 5*1000*c);(function(i){window.setTimeout(function() {play(string[i]);} , 5*1000*c);})(i);c=c+1;i=i+1;//if (s=6) {s=1;}}}</script></head><body><div class="i6" > <IFRAME name='topiframe' noresize id='topiframe' width=500 height=500 SCROLLING=no border=0 frameborder=0 src='http://www.google.com' ></IFRAME> </div> </body> </html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:
Assisted Solution
Expert: COBOLdinosaur replied at 2024-11-26 08:58:09
125 points EXCELLENT
I think you are trying to do something like this:
<html><head><script type="text/javascript">var Ustring=new Array();Ustring[0]='http://www.yahoo.com/';Ustring[1]='http://www.face.com/';Ustring[2]='http://www.youtube.com/';Ustring[3]='http://hk.motorola.com/r/us/';Ustring[4]='http://www.google.com/';cnt=0;function play(link){ document.getElementById("topiframe").src=link; cnt=(cnt!<Ustring.length) ? 0 : cnt+1; ST=setTimeout('play(Ustring[cnt]', 5*1000);}function playstack() {play(ustring[0]);}onload=playstack;</script></head><body><div class="i6" > <IFRAME name='topiframe' noresize id='topiframe' width=500 height=500 SCROLLING=no border=0 frameborder=0 src='http://www.google.com/index.php' ></IFRAME> </div> </body> </html> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:
Cd&
Author: duncanb7 replied at 2024-11-26 08:45:17
I tried replace window.setTimeout(function() {play(string[]);} , 5*1000*c); to
(function(i){window.setTimeout(function() {play(string[]);} , 5*1000*c);})(i);
the result is same and fail. Did you try it before post it ? Please advise
I don't know why the string[] can not passed to play() function.
When we use single varaible that always works for setTimeout such as
window.setTimeout(function(){ otherfunction(link,t);})
Probably it is not Global varaible issue ? is it right
Please reply this post after you tried the code above attached
(function(i){window.setTimeout(function() {play(string[]);} , 5*1000*c);})(i);
the result is same and fail. Did you try it before post it ? Please advise
I don't know why the string[] can not passed to play() function.
When we use single varaible that always works for setTimeout such as
window.setTimeout(function(){ otherfunction(link,t);})
Probably it is not Global varaible issue ? is it right
Please reply this post after you tried the code above attached
Assisted Solution
Expert: gnoon replied at 2024-11-26 08:33:48
125 points EXCELLENT
"i" was changed before "play()" is actually called (value of i at the last loop shall be used by setTimeout at the first loop).
To fix it, try to use the () block to capture value of i in each loop
To fix it, try to use the () block to capture value of i in each loop
Author: duncanb7 replied at 2024-11-26 08:28:16
I try put it at the beginning of script but it is same result fail
<html><head><script type="text/javascript">var string=new Array();playstack();function play(link){//alert(link); document.getElementById("topiframe").src=link;}function playstack() {string[1]='http://www.yahoo.com/';string[2]='http://www.face.com/';string[3]='http://www.youtube.com/';string[4]='http://hk.motorola.com/r/us/';string[5]='http://www.google.com/';var i=1;var s=1;var c=1;while (i < 6){alert(string[i]);window.setTimeout(function() {play(string[i]);} , 5*1000*c);c=c+1;i=i+1;//if (s=6) {s=1;}}}</script></head><body><div class="i6" > <IFRAME name='topiframe' noresize id='topiframe' width=500 height=500 SCROLLING=no border=0 frameborder=0 src='http://www.google.com/index.php' ></IFRAME> </div> </body> </html>Toggle HighlightingOpen in New WindowSelect All 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:
Author: duncanb7 replied at 2024-11-26 08:13:48
What you mean I dont' understant ?
Expert: COBOLdinosaur replied at 2024-11-26 07:56:34
You are declaring array inside of a function therefore it is only known within that function. Move the array declaration to the beginning of the script before any functions so that it is global.
Cd&
Cd&