473,732 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setTimeout not working on Mac IE 5.2

Could someone shed some light as to why the following setTimeout
function will not work on the Mac IE5.2? It does however work on
PC(Forefox,Nets cape,IE) & Mac(Safari,Fire fox). Here is the script, it
should simply present an alert message after 5 seconds. Thanks.

<html>
<head>
<script language="javas cript">
function MsgTimer() {
var self = this;
var mytimer;
var seccnt = 0;

// Private Method
var thetimer = function() {
if (seccnt >= self.secs) {
clearTimeout(my timer);
alert("Waited: " + self.secs + " seconds to show you: " +
self.msg);
} else {
seccnt++;
mytimer = setTimeout(thet imer,1000);
}
}

// Public Method
self.showmsg = function(msg,se cs) {
self.msg = msg;
self.secs = secs;
thetimer();
}

}
</script>
</head>

<body>

<h3>Simple Timer Function</h3>
<script language="javas cript">
var mt = new MsgTimer();
mt.showmsg('Hel lo World',5);
</script>

</body>
</html>

Jul 23 '05 #1
2 4668
Athanasius wrote:
Could someone shed some light as to why the following setTimeout
function will not work on the Mac IE5.2? It does however work on
PC(Forefox,Nets cape,IE) & Mac(Safari,Fire fox). Here is the script, it
should simply present an alert message after 5 seconds. Thanks.

<html>
<head>
<script language="javas cript">
function MsgTimer() {
var self = this;
var mytimer;
var seccnt = 0;

// Private Method
var thetimer = function() {
if (seccnt >= self.secs) {
clearTimeout(my timer);
alert("Waited: " + self.secs + " seconds to show you: " +
self.msg);
} else {
seccnt++;
mytimer = setTimeout(thet imer,1000);
}
}

// Public Method
self.showmsg = function(msg,se cs) {
self.msg = msg;
self.secs = secs;
thetimer();
}

}
</script>
</head>

<body>

<h3>Simple Timer Function</h3>
<script language="javas cript">
var mt = new MsgTimer();
mt.showmsg('Hel lo World',5);
</script>

</body>
</html>


No idea, but maybe a browse here may help:

<URL:http://www.dhtmlcentra l.com/forums/topic.asp?TOPIC _ID=17585&which page=2>
--
Fred
Jul 23 '05 #2
Athanasius wrote:
Could someone shed some light as to why the following
setTimeout function will not work on the Mac IE5.2? <snip> var thetimer = function() {
if (seccnt >= self.secs) {
clearTimeout(my timer);
alert("Waited: " + self.secs + " seconds to show you: " +
self.msg);
} else {
seccnt++;
mytimer = setTimeout(thet imer,1000);

<snip>

Early setTimeout implementations took a string as their first argument
and (when the interval timed-out (approximately) ) evaluated that string.
More recent implementations added the ability to use a reference to a
function object as the first argument, such that the function was
executed at the end of the interval. Windows IE versions added support
for this second option with the release if IE 5.0. I don't recall the
situation with Mac IE but as your are using a function reference as the
first argument it is possible that this is not working because that Mac
IE version's setTimeout only supports string arguments.

There are two strategies for handling this particular incompatibility .
The first, and most obvious, is to only use string arguments with
setTimeout. That will not help you at all in this context as your are
using a reference to an inner function and the string arguments to
setTimeout are evaluated in the global context (where the inner function
is invisible). It would also be a pity to be abandoning the use of the
more efficient function references with setTimeout on more recent
browsers just for the sake of supporting older browsers (especially when
there is an alternative).

The second strategy appears to have been noticed (invented?) a couple of
years ago, and takes advantage of javascript's tendency to type-convert
values into the type required for a particular context. When setTimeout
only supports string arguments it type-converts whatever argument it is
given into a string, and then evaluates that string. Type-converting a
Function reference into a string would involve calling the function
object's - toString - method. By default that method is the one defined
by - Function.protot ype.toString -, and returning an implementation
dependant value that is normally a normalised version of the function's
definition text. So it would be that text that was evaluated by the
setTimeout function. Evaluating that text will not result in an error,
but it does not result in the calling of the function (it actually
represents the evaluation of a function expression).

The trick is to replace the function's - toString - method with an
alternative that will result in the execution of the function. For
example:

function demo(){
// function body.
}
demo.toString = function(){
return 'demo();';
};
setTimeout(demo , 400);

- passes a function reference to - setTimeout - and so takes advantage
of the more efficient execution of function references available on more
recent browsers. On a browser that only supports string values as the
first argument to - setTimeout - the function reference is
type-converted into a string by calling its - toString - method, which
returns the string "demo();", and that is a function call. The function
is called regardless of the - setTimeout - implementation, and in the
most efficient way available.

However, this will not handle your inner function problem because the
string returned form the call to the function's - toStirng - method
would still be evaluated in the global context. That is not a problem in
the 'demo' example above because the - demo - function is defined in the
global context. This is not an insurmountable problem. The solution is
to have the - toString - method also expose the inner function in the
global context. One approach might be:-

function example(){
function exampleInner(){
// function body;
}
exampleInner.to String = globalFuncToStr ing;
setTimeout(exam pleInner, 400);
}

function globalFuncToStr ing(){
globalFuncToStr ing.execut = this; //this - is a reference to the inn
er function
return 'globalFuncToSt ring.execut();' ;
}

- where a global function is assigned as the - to String - method of the
inner function. When that global function is called, during the
type-conversion process, it is a executed as a method of the inner
function so the - this - keyword is a reference to that inner function.
A reference to that inner function is assigned (using - this-) to a
public property of the global function and the returned string is code
that calls the inner function as a method of the global function (which
is not significant as the inner function cannot usefully employ the -
this - keyword itself).

The above would work, but only with one setTimeout at a time, which is
probably not satisfactory. The problem is that there is only one
globally accessible reference being used so if two or more setTimout's
overlapped they would tend to execute only the last function assigned
(and setTieouts are likely to overlap).

A more elaborate global function used as inner function's - toString -
methods might adopt a more flexible approach, creating a separate
globally accessible property for each function used. E.G:-

function example(){
function exampleInner(){
// function body;
}
exampleInner.to String = globalToString;
setTimeout(exam pleInner, 400);
}

function globalToString( ){
if(!globalToStr ing.ex){
globalToString. ex = [];
}
var c = globalToString. ex.length;
var st = 'globalToString .ex['+c+']();';
globalToString. ex[c] = this;
return (this.toString = function(){
return st;
})();
}

In this case each function that uses this globalToString function
creates a new entry in an array, and then replaces the function's to
string method with a function that returns a string that calls the
function as an indexed element of that array. So many functions will
work with this version at the same time, and the same function
repeatedly without any additional work.

The drawback with this version is that all of those function references
remain assigned as elements of the array. If they are inner functions
then the resulting closures will be indefinitely preserved.

An alternative is to make the event scheduling the responsibility of a
separate external function/object and have that function do the work of
providing the fall-back for older setTimout implementations . The inner
function references would be passed to that function/object and it would
schedule their execution. I frequently use do this for dynamic animation
in scripts, which is similar to your original requirement to execute an
inner function at one second intervals, except that for animation the
intervals must be shorter. This is the current version of the animation
timer that I use:-

var TimedQue = (function(){
var base, timer;
var interval = 42;
var newFncs = null;
function addFnc(next, f){
function t(){
next = next&&next();
if(f()){
return t;
}else{
f = null;
return next;
}
};
t.addItem = function(d){
if(next){
next.addItem(d) ;
}else{
next = d;
}
return this;
};
return t;
};
function tmQue(fc){
if(newFncs){
newFncs = newFncs.addItem (addFnc(null, fc));
}else{
newFncs = addFnc(null, fc);
}
if(!timer){
timer = setTimeout(tmQu e.act, interval);
}
};
tmQue.act = function(){
var fn = newFncs, strt = new Date().getTime( );
if(fn){
newFncs = null;
if(base){
base.addItem(fn );
}else{
base = fn;
}
}
base = base&&base();
if(base||newFnc s){
var t = interval - (new Date().getTime( ) - strt);
timer = setTimeout(tmQu e.act, ((t > 0)?t:1));
}else{
timer = null;
};
};
tmQue.act.toStr ing = function(){
return 'TimedQue.act() ';
};
return tmQue;
})();

It is called as:-

TimedQue(functi onReference);

- where - functionReferen ce - is always a reference to a function. That
function will be executed at intervals defined by the - interval -
variable, and any number of functions can be added to the process (all
will be executed at the specified interval). The contract for those
functions passed by reference to the - TimedQue - function is that they
return true for as long as they want to continue being executed, and
they return false when they no longer want to be executed at the
specified interval. Thus the function references are freed when they
leave the scheduling process.

The - TimedQue - function handles the fall-back for string-argument-only
setTimeout implementations itself, so the referenced functions do not
have to implement it themselves.

It is because of the use of this strategy that I do not remember whether
Mac IE 5 supports function references with the setTimeout function. My
code would work regardless of the browser's implementation of
setTimeout, and still keeps the simplicity of using function references
(including inner functions) so I don't need to remember.

Richard.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14943
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);
5
2576
by: oliver | last post by:
Hi, the structure of my source code is like this: <script> C = function(){ //some initialization } C.prototype.start = function{ //some action
3
8593
by: jshanman | last post by:
I've created a "Play" button for my timeline that uses the Google Maps API. It basicly scrolls the timeline x interval every y milliseconds. The problem is the browser responds slowly even after the "Stop" button is clicked. (it responds slowly while playing, but thats to be expected with everything that it is doing) How can I get the browser back to it's more responsive state after the timeout is cleared? Here is the function that...
12
5555
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
28
5306
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
1
7105
by: James Black | last post by:
I am trying to fade a word out, so after 5 seconds then every 1/4 sec it should fade by a small amount, until it is gone. This works fine in Firefox, but IE never calls the function that setTimeout points to. I am using IE 6, btw, on WinXP SP2. browserdetect=savedspan.filters? "ie" : typeof savedspan.style.MozOpacity=="string"? "mozilla" : "";
17
2413
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) } and call it 50 times, will it cause problems because you are not
4
5234
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
7
2415
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to interfer with one another.
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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...
1
9235
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
9181
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
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
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
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.