• js替换元素与设置时间间隔


    var lastReportTime = 0;
    
    //设置时间间隔
    window.onload = function(){
        setInterval(handleRefresh, 3000);
    }
    
    function handleRefresh(){
        var url = "http://gumball.wickedlysmart.com" +
            "?callback=updateSales" +
            "&lastreporttime=" + lastReportTime +
            "&random=" + (new Date()).getTime();
        var newScriptElement = document.createElement("script");
        newScriptElement.setAttribute("src", url);
        newScriptElement.setAttribute("id","jsonp");
    
        var oldScriptElement = document.getElementById("jsonp");
        var head = document.getElementsByTagName("head")[0];
        if (oldScriptElement == null){
            head.appendChild(newScriptElement)
        } else {
            //替换元素
            head.replaceChild(newScriptElement, oldScriptElement);
        }
    }
    
    function updateSales(sales){
        var salesDiv = document.getElementById("sales");
        for (var i = 0; i < sales.length; i++){
            var sale = sales[i];
            var div = document.createElement("div");
            div.setAttribute("class", "saleItem");
            div.innerHTML = sale.name + " sold " + sale.sales + " gumballs";
            salesDiv.appendChild(div);
        }
        if (sales.length > 0){
            lastReportTime = sales[sales.length - 1].time;
        }
    
    }
  • 相关阅读:
    react-native 点击按钮进行交互
    常用类
    js里面 undefined 和 null
    ajax 提交数据
    通过Unicode判断一个字符是不是汉字
    git commit 后的对话框
    vue-cli 使用less的方法
    node创建服务器简单测试接口
    bootstrap3 模态框js的控制
    鱼眼镜头的坐标变换
  • 原文地址:https://www.cnblogs.com/themost/p/9434571.html
Copyright © 2020-2023  润新知