tags:

views:

216

answers:

3

Hello,

I am new to javascript and jquery, i load the data from another site through php but i want to refresh it every minute without use of php because they data returned is in this format 1000, 10000 offcourse the numbers are different everytime.

So how can i load the data every minute and split it, like in php i use explode(",", "1000, 10000").

I want to split the data to display the % so in this example 1000/10000 * 100 will give us 10%.

Thank You.

A: 

If I understand your question correctly, you've already figured out how to load the data to your page, and now you just want to parse it and make use of it.

In that case, you really don't need any jquery, you can just use the javascript split method.

var dataFromOtherUrl = "1000, 10000";
var result = dataFromOtherUrl.split(", ") // result is the array: ["1000", "10000"]

From there you can use the values in the array to do your % math:

var percentage = result[0]/result[1] * 100;

If you want to limit the # of times the string is split you can pass another argument to the method.

TM
nope i dont know how to run the function every minute
Shishant
+3  A: 

What you are looking for is the string split() function. You can use it to take a string and split based on a regular expression. You can then take the returned substrings and convert them to numbers in order to do the math on them. Note that you may be able to omit the explicit conversion, but sometimes numbers are treated differently than strings (for example the + operator will append strings even if they are all digits) so I feel that it's a good practice.

 var nums = data.split( /,/ );

 var pct = Number(nums[0]) / Number(nums[1]) * 100;

To get the data every minute you'll need to use AJAX and call the php method that generates the numbers. Note that either the url needs to be in the same domain or you need to use JSONP to do the transfer. I'll assume (since you are working with a string) that the value is not JSON, but raw content. If you can use JSON, you wouldn't need to do string splitting since you can put the numbers in individual properties of the JSON object.

 $(function() {
    setInterval( function() {
       $.ajax({
          url: 'some.php',
          dataType: 'html',
          success: function(data) {
               var nums = data.split( /,/ );
               $('#percentage').text( (Number(nums[0]) / Number(nums[1]) * 100) + '%' );
          }
       });
    },60000);
 });
tvanfosson
But can you please tell me how to run the function every minute
Shishant
+1 for passing function into setInterval... don't understand why people insist on using strings like "someMethod()" when functions or function names work.
TM
A: 

To run a function every minute you have to use the setInterval-Methode.

var activ = window.setInterval("update()", 1000*60);
function update() {
  // your update code goes here
  // Ajax-Request
  // parsing
  // updating the display
}

to stop it, use:

window.clearInterval(activ);
crono