Category Archives: Examples

Jquery use queue to implement Ajax request queue Simple Example

Packaging method

var axmq = {
    //Queues
    queues: [],
    //network request
    request: null,
    //Execution queue
    render: function() {
        $(document).queue(axmq.queues);
    },
    //append queue
    append: function(func) {
        axmq.queues.push(func);
    },
    //clear queue
    clear: function() {
        $(document).dequeue();
        if (0 === $(document).queue().length) {
            axmq.queues = [];
            $(document).clearQueue();
        }
    },
    //POST request
    post: function(args) {
        var params = {
            url: 'https://www.sample.com/api',
            headers: {},
            data: {},
            buffer: function() {},
            callback: function() {}
        };
        $.extend(params, args);
        var headers = {
            Accept: 'application/json;charset=utf-8'
        };
        if (Object.keys(params.headers).length > 0) {
            $.extend(headers, params.headers);
        }
        if (axmq.request == null) {
            axmq.request = $.ajax({
                async: true,
                type: 'POST',
                url: params.url,
                headers: headers,
                data: params.data,
                dataType: 'JSON',
                beforeSend: function() {
                    params.buffer();
                },
                success: function(res) {
                    console.log(res);
                    axmq.request = null;
                    axmq.clear();
                    params.callback(res);
                },
                error: function(err) {
                    console.log(err);
                    axmq.request = null;
                    axmq.clear();
                    params.callback({
                        errcode: 5001,
                        errmsg: 'System busy'
                    });
                }
            });
        }
    },
    //example
    sample: function() {
        axmq.append(function() {
            axmq.post({
                url: 'https://www.sample.com/api/a'
            });
        });
        axmq.append(function() {
            axmq.post({
                url: 'https://www.sample.com/api/b'
            });
        });
        axmq.append(function() {
            axmq.post({
                url: 'https://www.sample.com/api/c'
            });
        });
        axmq.render();
    }
};

Call example

axmq.sample();

MySQL Batch Add Data and Store Example

DELIMITER $$
-- If the procedure already exists, delete it
DROP PROCEDURE IF EXISTS SAVERANDDATA;
-- Create procedure name
CREATE PROCEDURE SAVERANDDATA()
BEGIN
	DECLARE i INT;
	SET i = 0;
	-- Defining the MYSQL REPEAT Statement
	REPEAT
		-- SQL content
		INSERT INTO `lzhstore`.`almart_all` (
			`date_key`,
			`hour_key`,
			`client_key`,
			`item_key`,
			`account`,
			`expense`
		)
		VALUES
		(
			"2016-05-01",
			FLOOR(RAND() * 24),
			FLOOR(RAND() * 1000000) + 1,
			FLOOR(RAND() * 100000) + 1,
			FLOOR(RAND() * 20) + 1,
			FLOOR(RAND() * 10000) + 1
		);	
	SET i = i + 1;
  -- Ends the loop if executed one million times
  UNTIL i = 1000000 END REPEAT;
	END $$
	CALL SAVERANDDATA();

go sync.Mutex Lock Examples

package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	m = make(map[int]uint64)
	lock sync.Mutex
)

type task struct {
	n int
}

func calc(t *task) {
	var sum uint64
	sum = 1
	for i := 1; i <= t.n; i++ {
		sum *= uint64(i)
	}
	lock.Lock()
	m[t.n] = sum
	lock.Unlock()
}

func main() {
	for i := 0; i < 10; i++ {
		t := &task{n:i}
		go calc(t)
	}
	time.Sleep(1 * time.Second)

	lock.Lock()
	for k, v := range m {
		fmt.Printf("%d! = %v\n", k, v)
	}
}

 

Echarts-for-React Example (Install, Import and Effect)

Install

npm install echarts-for-react –save

 

Import

import ReactEcharts from “echarts-for-react”;

 

Example Codes:

import React from 'react'
import ReactEcharts from 'echarts-for-react'
class Demon extends React.Component {
  state = {option: {}};
  render () {
    return (
      <>
        <ReactEcharts option={this.state.option} />
      </>
    );
  }
  componentDidMount () {
    let option = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [120, 200, 150, 80, 70, 110, 130],
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          }
        }
      ]
    };
    this.setState({option})
  }
}
export default Demon;

 

Effect:

 

Note:

The x-axis in the grid of the xAxis indicator

yAxis indicator coordinate system y-axis in grid

series series chart configuration He decides which type of chart to display