简体   繁体   English

如何将int数据从数据库检索到JavaScript中的变量?

[英]How to retrive int data from database to a variable in javascript?

I have a code in asp.net page for my progress bar i'm having a back end code to retrieve percentage my problem is how to retrieve that int value to my java script here is the code for progress bar is 我在asp.net页面的进度条中有一个代码,我有一个后端代码来检索百分比,我的问题是如何将int值检索到我的Java脚本中,这是进度条的代码是

<div class="w3-wow1" style="font-size:x-large">Total Potential</div>

         <div class="w3-padding-32"></div>
          <span class="w3-wow2" style="font-size:x-large">Potential Till Date :</span>
     <button class="w3-btn w3-white w3-round-xlarge" onclick="move1()">Click Here</button> 
        <div class="w3-padding-8"></div>
     <div class="w3-progress-container  w3-round-xlarge" style="height:40px">
     <div id="myBar1" class="w3-progressbar w3-round-xlarge w3-green " style="width:0%">
     <div id="per1" class="w3-center w3-text-white" style="margin-top:10px">0%</div></div>            
     </div>

and javascript code for this one is 这个的JavaScript代码是

 <script>
     function move1() {
         var elem = document.getElementById("myBar1");

       //var per= int data from database

         var width = 1;
         var id = setInterval(frame, 10);
         function frame() {
             if (width >= 80) { // here in the place of 80 i want to use 'per' variable to assign the progress bar
                 clearInterval(id);
             } else {
                 width++;
                 elem.style.width = width + '%';
                 document.getElementById("per1").innerHTML = width * 1 + '%';
             }
         }
     }</script>

the backend code for this one is 这个的后端代码是

    protected void potent()
    {
        string query = "select ( sum(oppfp /100000)* 100.0)AS percentage from opprt";

        using (SqlConnection con = new SqlConnection("Data Source=PO-PC\\SQLEXPRESS;Initial Catalog=leadsp;User ID=sa;Password=123;"))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;

                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        ListItem item = new ListItem();
                        string per = sdr["percentage"].ToString();
                        int percentage = Convert.ToInt32(per);

                    }
                }
                con.Close();
            }
        }
    }

how to retrieve this data as int and pass it to per variable in java script 如何以int形式获取此数据并将其传递给Java脚本中的每个变量

As Khalid said you can use an ajax call to the server to retrive the update value. 正如Khalid所说,您可以使用对服务器的Ajax调用来获取更新值。 You can use a timer to call the site every second as long as you need it. 您可以根据需要使用计时器每秒调用一次该站点。

A simple example could be: 一个简单的例子可能是:

var myTimer = setTimeout(function(){ 
  $.ajax(siteUrl, {
    success: function(data) {
       // do your thing with the downloaded data
    },
    error: function(xhr,status,error) {
       alert(error)
    }
  });
, 1000);

When you are done you can stop the timer with: 完成后,您可以使用以下方法停止计时器:

clearTimeout(myTimer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM