Js automatically play the instance code

  • 2020-03-29 23:50:11
  • OfStack


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>auto play</title> 
<script type="text/javascript" src="../jquery-1.8.0.min.js" /></script> 
<script type="text/javascript"> 
    //The first item in the array is the length. The other items are customizable
    data = [["3","a"],["2","b"],["3","c"],["2","d"],["1",""]]; 

    $(document).ready(function(){ 
        //Set the trigger event
        autoPlay.run = function(obj,num){ 
            back(obj,num); 
        }; 
        autoPlay(); 
    }); 

    function back(obj,num){ 
        $("#div_show").html(obj[num][1]); 
    } 

    function autoPlay(){ 
        //Initializes the count parameter
        if(autoPlay.num ==undefined) 
            autoPlay.num = 0; 
        //Initialize the time point
        if(autoPlay.timepoint ==undefined) 
            autoPlay.timepoint = new Date().getTime(); 
        if(autoPlay.num<data.length){ 
            //Function handle, data array, autoplay.num current index
            autoPlay.run(data,autoPlay.num); 
            //Play the array by element time
            if(new Date().getTime()>autoPlay.timepoint){ 
                //Determine if this is the first execution
                if(autoPlay.first==undefined) 
                    autoPlay.first = true; 
                else 
                    autoPlay.num++; 
                //Set the time point for the next playback
                autoPlay.timepoint += (data[autoPlay.num][0]*1000); 
            } 
            setTimeout("autoPlay()",500); 
        } 
    } 

    $.extend({autoPlay:function(){ 
        var defaults = {inteval:500}; 
    }}); 

</script> 
</head> 
<body> 
<div id="div_show"></div> 
</body> 
</html> 

Define the play time and you can define events


Related articles: