Javascript: setTimeout()使用及 setInterval()使用

    技术2022-05-12  8

    Javascript: setTimeout()使用及 setInterval()使用 2006-10-12 03:36

    Evaluates an expression after a specified number of milliseconds has elapsed.



    iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])


    vCodeRequired. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed.iMilliSecondsRequired. Integer that specifies the number of milliseconds.sLanguageOptional. String that specifies one of the following values: JScriptLanguage is JScript.VBScriptLanguage is VBScript.JavaScriptLanguage is JavaScript.

    Return Value

    Integer. Returns an identifier that cancels the evaluation with the clearTimeout method.




    -------------------------------------------------------------------setTimeout( alert("3秒种过去了"), 3000);//调用一个函数,允许带常量参数-------------------------------------------------------------------<script language="Javascript">//by zuoyangvar x = 1;var y = 2;var z = 3;var sum;function Plus(a, b){          var z = 0;          var i = 0;          for (i = 0; i < arguments.length; i++)          {                   z += arguments[i];          }          setTimeout( function() {alert(z);}, 6000); //可以带变量参数的setTimeout调用形式          return z;}setTimeout( function(){ sum = Plus(x, y, z); }, 3000);/*除了可以带变量参数还可以获取返回值的setTimeout调用形式*/</script>



    iTimerID = window.setInterval(vCode, iMilliSeconds [, sLanguage])

    不同的是setTimeout()是一次性作用,而setInterval()是每隔iMilliSeconds就执行一次vCode.(Evaluates an expression each time a specified number of milliseconds has elapsed)