Nested for loop execution order. The internal and external for loop can be executed alternately. Simple parsing of JSON arrays. The two time difference values in JS are obtained.

Source: Internet
Author: User
Tags drawtext

1. Shortly after the task was received, it contained a parse JSON array, and then let the parsed data appear on the rectangle drawn with Zrender. The effect is as follows (note: no practical significance)

2. As for zrender and so idle must write on the use of experience, things are very strong.

3. One of the middle is calculating the time difference between two adjacent times (all of which are in an array), calculating the difference between the Times on the adjacent two rectangles. The extension can be calculated as the difference between the two digits in an array.

Paste My Code: for (var m = 0; m < dataobj.length-1; m++) {
for (var j = m + 1; j < Dataobj.length; J + +) {
var time = Getdatediff (Dataobj[m].datetime, Dataobj[j].datetime, "minute");
Addshapetext (Timexx, Timeyy, Time + "minutes", ' normal 10px Verdana ');
Timexx + = 180;
Break
}
}

Wrote the first morning found that the error, until later learned that the For loop is to perform the outer loop, then the memory loop, the inner loop, until the inner for loop does not meet the conditions to perform the outer loop. That's the problem. This is not the case, but the inner and outer for loop is executed alternately. "An epiphany: at the end of the inner for loop add break;" resolve the problem decisively, haha.

4. There is a addshapetext in the middle (Timexx, timeyy, Time + "minutes", ' normal 10px Verdana '); In fact Addshapetext This method is online when, mainly used to calculate the two time difference in JS, the specific address is not clear, paste the code record, for later use:/*
* Get the time difference, the format is year-month-day hour: minutes: seconds or Year/month/day hour: minutes: seconds
* The date of the month is the full format, for example: 2010-10-12 01:00:00
* Return accuracy: seconds, minutes, hours, days
*/
function Getdatediff (startTime, EndTime, Difftype) {
Convert xxxx-xx-xx time format to XXXX/XX/XX format
StartTime = Starttime.replace (/\-/g, "/");
EndTime = Endtime.replace (/\-/g, "/");

Convert a calculated interval class character to lowercase
Difftype = Difftype.tolowercase ();
var stime = new Date (startTime); Start time
var etime = new Date (endTime); End time
Number as the divisor
var divnum = 1;
Switch (difftype) {
Case "Second":
Divnum = 1000;
Break
Case "Minute":
Divnum = 1000 * 60;
Break
Case "Hour":
divnum = 1000 * 3600;
Break
Case ' Day ':
divnum = 1000 * 3600 * 24;
Break
Default
Break
}
Return parseint ((Etime.gettime ()-stime.gettime ())/parseint (divnum));
}
5. Parse the JSON array, the sample code is as follows:

$ (function () {

JSON array
var title = ["AA",
"BB", "CC",
"DD", "EE",
"FF"]
DrawText (title);
})

function DrawText (data) {
var dataobj = eval (data);
for (var i = 0; i < dataobj.length; i++) {

Loops through the output of each value in the array.
Alert (Dataobj[i]);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.