• JS中location.search和setTimeout()和 setInterval()


    JS中location.search什么意思

     

    设置或获取 网页地址跟在问号后面的部分

    当以get方式在url中传递了请求参数时,可以利用location的search属性提取参数的值,下面的代码把参数的名称和对应的值存储在2个数组中。

    <script>
    function test()
    {
    var url=window.location.search;
    if(url.indexOf("?")!=-1) 

       var str = url.substr(1) 
        strs = str.split("&"); 
        var key=new Array(strs.length);
        var value=new Array(strs.length);
        for(i=0;i<strs.length;i++) 
        { 
         key[i]=strs[i].split("=")[0]
         value[i]=unescape(strs[i].split("=")[1]); 
        alert(key[i]+"="+value[i]);
       } 

    }
    </script>

    --------------------------------------------------------------

    <html>
    <head><title>none</title>
    <script>
    function getParam(name){
    var params=location.search.substring(1).toLowerCase();alert('params='+params);
    var paramList=[];
    var param=null;
    var parami;
    if(params.length>0) {
    if(params.indexOf("&") >=0) {
       paramList=params.split( "&" ); 
    }else {
       paramList[0] = params;
    }
    for(var i=0,listLength = paramList.length;i<listLength;i++) {
       parami = paramList[i].indexOf(name+"=" );
       if(parami>=0) {
        param =paramList[i].substr(parami+(name+"=").length);
       }
    }
    }
    return param;
    }
    function check(){
    id=document.getElementById('test').value;alert('one'+id);
    var userid=getParam(id);alert('two'+userid);

    var flag=false;
    var str='2010001,2010002,2010003,2010004,2010005,2010006,2010007,2010008,2010009,2010010,2010011,2010012,2010013,2010014,2010015,2010016,2010017,2010018,2010019,2010020,2010021,2010022,2010023,2010024,2010025,2010026,2010027,2010028,2010029,2010030,2010031,2010032,2010033,2010034,2010035,2010036,2010037,2010038,2010039,2010040,2010041,2010042,2010043,2010044,2010045,2010046,2010047,2010048,2010049,2010050,2010051,2010052,2010053,2010054,2010055,2010056,2010057,2010058,2010059,2010060,2010061,2010062,2010063,2010064,2010065,2010066,2010067,2010068,2010069,2010070,2010071,2010072,2010073,2010074,2010075,2010076,2010077,2010078,2010079,2010080,2010081,2010082,2010083,2010084,2010085,2010086,2010087,2010088,2010089,2010090,2010091,2010092,2010093,2010094,2010095,2010096,2010097,2010098,2010099';
    var arr=str.split(',');
    for(var i=0,l=arr.length;i<l;i++){
    if(userid == arr[i])
    {
       flag=true;
       break;
    }
    }
    if(!flag){
    alert('demine!');
    return false;
    }else{
    alert('right');
    }
    }
    </script>
    </head>
    <body>
    <table >
    <form>
    请输入<input type="text" name="test" id="test"/>
    <input type="button" value="确认" onclick="javascript:check();"/>
    </form>
    </table>
    </body>
    </html>

    setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。setTimeout()执行到这句代码的时候暂停

    setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()。

    1.只执行一次的定时器

    <script> 
    //定时器 异步运行 
    function hello(){ 
    alert("hello"); 

    //使用方法名字执行方法 
    var t1 = window.setTimeout(hello,1000); 
    var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法 
    window.clearTimeout(t1);//去掉定时器 
    </script> 

    2.重复执行的定时器

    <script> 
    function hello(){ 
    alert("hello"); 

    //重复执行某个方法 
    var t1 = window.setInterval(hello,1000); 
    var t2 = window.setInterval("hello()",3000); 
    //去掉定时器的方法 
    window.clearInterval(t1); 
    </script> 

  • 相关阅读:
    fabric.js 学习
    前端基础回顾
    ubuntu16 mysql 远程连接
    Django rest_framework API 随笔
    django 学习笔记
    vue 返回上一页在原来的位置
    Django mysql 改用pymysql 驱动
    Material-UI
    设置placeholder的样式
    Cookie、session和localStorage、以及sessionStorage之间的区别
  • 原文地址:https://www.cnblogs.com/tangyue/p/3709420.html
Copyright © 2020-2023  润新知