JS Dom settimeout and setinterval different browser compatibility tests (1/2)

Source: Internet
Author: User
Tags setinterval

<title>settimeout example</title>

<script type= "Text/javascript Tutorial" >
function Delayedalert ()
{
Timeoutid = Window.settimeout (Slowalert, 2000);
}

function Slowalert ()
{
Alert ("That is really slow!");
}

function Clearalert ()
{
Window.cleartimeout (Timeoutid);
}

Window.settimeout (' Window.parent.generateOutput () ', 1000);
function Generateoutput (aconcise) {
if (aconcise)
Parent.generateconciseoutput ();
Else
Parent.generateoutput ();
}
Window.settimeout (Generateoutput, 1000, true);

</script>

<body>
<button onclick= "Delayedalert ();"
>show an alert box after 2 seconds</button><br>
<button onclick= "Clearalert ();" >Cancel</button>
<!--
Tips for using settimeout

(1) The call function pass parameter in IE to the settimeout
The above analysis shows that IE is not supported in the settimeout to the called function parameters, for the sake of the harmony of the browser world, we can wrap the function call parameters into the new anonymous function. Example:
function f (a) {
alert (a);
}
settimeout (f,50, ' hello '); For non-IE
settimeout (function () {f (' hello ')},50); General
var str= ' Hello ';
settimeout (function () {f (str)},50); General

(2) This question
The settimeout called function is executed when the context is global, not the context when the SetTimeout method is invoked. So, when the function of the first parameter of settimeout is executed, it points to window, and if you need to keep this when you call the SetTimeout method, then you need to pass this in. Example:
function person (name) {
This.name=name;
var f=function () {alert (' My name is ' +this.name)};

SetTimeout (f,50); Error

home 1 2 last

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.