那些jquery或javascript花招

  • js定时器
定时器:
1)反复性定时器:格式:window.setInterval(“fn()”,1000);
a)会反复执行
b)第二个参数是以毫秒计算的
2)一次性定时器:格式:window.setTimeout(“fn()”,1000);
a)执行一次
b)第二个参数是以毫秒计算的
清除定时器:
1)window.clearInterval();清除反复性定时器
2)window.clearTimeout();清除一次性定时器
  • 一个显示上传图片的案例
  #html部分
<div class="col-md-6"> <img id='showpics' class="featurette-image img-fluid mx-auto" data-src="holder.js/500x500/auto" alt="Generic placeholder image" > <form> <div class="form-group row"> <label for="goodsname" class="col-sm-2 col-form-label"> <button type="button" id="upbtn" class="btn btn-primary">图片上传</button> </label> <div class="col-sm-10"> <input type="file" name="file0" id="file0" multiple="multiple" class="form-control" placeholder=""> </div> </div> </form> </div>
#jquery部分
<script src=".././static/booststrap4/js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    function getObjectURL(file)
    {
        var url = null ;
        if (window.createObjectURL!=undefined)
        { // basic
            url = window.createObjectURL(file) ;
        }
        else if (window.URL!=undefined)
        {
            // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
        }
        else if (window.webkitURL!=undefined) {
            // webkit or chrome
            url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }
    $("#file0").change(function(){
        var objUrl = getObjectURL(this.files[0]) ;
        console.log("objUrl = "+objUrl) ;
        if (objUrl)
        {
            $("#showpics").attr("src", objUrl);
            $("#showpics").removeClass("hide");
        }
    }) ;

</script>

 

posted @ 2018-12-22 15:46  saintdingtheGreat  阅读(132)  评论(0编辑  收藏  举报