728x90

This then passes the function without executing it first.

Here is an example:

function addContact(id, refreshCallback) {
    refreshCallback();
    // You can also pass arguments if you need to
    // refreshCallback(id);
}

function refreshContactList() {
    alert('Hello World');
}

addContact(1, refreshContactList);
728x90

The Uint8Array typed array represents an array of 8-bit unsigned integers. The contents are initialized to 0. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).

Constructor

Uint8Array()Creates a new Uint8Array object.

Static properties

Uint8Array.BYTES_PER_ELEMENTReturns a number value of the element size. 1 in the case of an Uint8Array.Uint8Array.nameReturns the string value of the constructor name. In the case of the Uint8Array type: "Uint8Array".

Static methods

Uint8Array.from()Creates a new Uint8Array from an array-like or iterable object. See also Array.from().Uint8Array.of()Creates a new Uint8Array with a variable number of arguments. See also Array.of().

Instance properties

Uint8Array.prototype.bufferReturns the ArrayBuffer referenced by the Uint8Array. Fixed at construction time and thus read only.Uint8Array.prototype.byteLengthReturns the length (in bytes) of the Uint8Array. Fixed at construction time and thus read only.Uint8Array.prototype.byteOffsetReturns the offset (in bytes) of the Uint8Array from the start of its ArrayBuffer. Fixed at construction time and thus read only.Uint8Array.prototype.lengthReturns the number of elements held in the Uint8Array. Fixed at construction time and thus read only.

Instance methods

Uint8Array.prototype.copyWithin()Copies a sequence of array elements within the array. See also Array.prototype.copyWithin().Uint8Array.prototype.entries()Returns a new array iterator object that contains the key/value pairs for each index in the array. See also Array.prototype.entries().Uint8Array.prototype.every()Tests whether all elements in the array pass the test provided by a function. See also Array.prototype.every().Uint8Array.prototype.fill()Fills all the elements of an array from a start index to an end index with a static value. See also Array.prototype.fill().Uint8Array.prototype.filter()Creates a new array with all of the elements of this array for which the provided filtering function returns true. See also Array.prototype.filter().Uint8Array.prototype.find()Returns the found value in the array, if an element in the array satisfies the provided testing function or undefined if not found. See also Array.prototype.find().Uint8Array.prototype.findIndex()Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found. See also Array.prototype.findIndex().Uint8Array.prototype.forEach()Calls a function for each element in the array. See also Array.prototype.forEach().Uint8Array.prototype.includes()Determines whether a typed array includes a certain element, returning true or false as appropriate. See also Array.prototype.includes().Uint8Array.prototype.indexOf()Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also Array.prototype.indexOf().Uint8Array.prototype.join()Joins all elements of an array into a string. See also Array.prototype.join().Uint8Array.prototype.keys()Returns a new array iterator that contains the keys for each index in the array. See also Array.prototype.keys().Uint8Array.prototype.lastIndexOf()Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also Array.prototype.lastIndexOf().Uint8Array.prototype.map()Creates a new array with the results of calling a provided function on every element in this array. See also Array.prototype.map().Uint8Array.prototype.reduce()Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. See also Array.prototype.reduce().Uint8Array.prototype.reduceRight()Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value. See also Array.prototype.reduceRight().Uint8Array.prototype.reverse()Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also Array.prototype.reverse().Uint8Array.prototype.set()Stores multiple values in the typed array, reading input values from a specified array.Uint8Array.prototype.slice()Extracts a section of an array and returns a new array. See also Array.prototype.slice().Uint8Array.prototype.some()Returns true if at least one element in this array satisfies the provided testing function. See also Array.prototype.some().Uint8Array.prototype.sort()Sorts the elements of an array in place and returns the array. See also Array.prototype.sort().Uint8Array.prototype.subarray()Returns a new Uint8Array from the given start and end element index.Uint8Array.prototype.values()Returns a new array iterator object that contains the values for each index in the array. See also Array.prototype.values().Uint8Array.prototype.toLocaleString()Returns a localized string representing the array and its elements. See also Array.prototype.toLocaleString().Uint8Array.prototype.toString()Returns a string representing the array and its elements. See also Array.prototype.toString().Uint8Array.prototype[@@iterator]()Returns a new array iterator object that contains the values for each index in the array.

Examples

Different ways to create a Uint8Array

// From a length var uint8 = new Uint8Array(2); uint8[0] = 42; console.log(uint8[0]); // 42 console.log(uint8.length); // 2 console.log(uint8.BYTES_PER_ELEMENT); // 1 // From an array var arr = new Uint8Array([21,31]); console.log(arr[1]); // 31 // From another TypedArray var x = new Uint8Array([21, 31]); var y = new Uint8Array(x); console.log(y[0]); // 21 // From an ArrayBuffer var buffer = new ArrayBuffer(8); var z = new Uint8Array(buffer, 1, 4); // From an iterable var iterable = function*(){ yield* [1,2,3]; }(); var uint8 = new Uint8Array(iterable); // Uint8Array[1, 2, 3]

Specifications

Specification

ECMAScript (ECMA-262)
The definition of 'TypedArray constructors' in that specification.

Browser compatibility

 

 

developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array

728x90

View: gettimeofday

Function:
Description: Returns the current time as array.

Code

function gettimeofday (return_float) {
    // http://jsphp.co/jsphp/fn/view/gettimeofday
    // + original by: Brett Zamir (http://brett-zamir.me)
    // +      derived from: Josh Fraser (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/)
    // +         parts by: Breaking Par Consulting Inc (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7)
    // +  revised by: Theriault
    // *   example 1: gettimeofday();
    // *   returns 1: {sec: 12, usec: 153000, minuteswest: -480, dsttime: 0}
    // *   example 1: gettimeofday(true);
    // *   returns 1: 1238748978.49
    var t = new Date(),
        y = 0;

    if (return_float) {
        return t.getTime() / 1000;
    }

    y = t.getFullYear(); // Store current year.
    return {
        sec: t.getUTCSeconds(),
        usec: t.getUTCMilliseconds() * 1000,
        minuteswest: t.getTimezoneOffset(),
        // Compare Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC to see if DST is observed.
        dsttime: 0 + (((new Date(y, 0)) - Date.UTC(y, 0)) !== ((new Date(y, 6)) - Date.UTC(y, 6)))
    };
}
728x90

차례1.1. 사용법1.2. 설명1.3. 반환값1.4. 에러1.5. 예제1.6. 참고문헌

현재 시간을 가져오고 시스템의 시간값을 설정한다.


1.1. 사용법

#include <sys/time.h>
#include <unistd.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv ,  const  struct
             timezone *tz);

 

1.2. 설명

gettimeofday()은 time(2)와 매우 비슷하지만 마이크로초 단위의 시간 까지 되돌려준다. 현재는 time(2)를 대신해서 쓰이고 있으며, 가능한 time(2)대신 이 함수를 사용하는 걸 권장한다.

첫번째 인자인 tv는 현재 시스템 시간을 저장하기 위한 구조체로 다음과 같이 정의되어 있다.

struct timeval
{
    long tv_sec;       // 초
    long tv_usec;      // 마이크로초
}

두번째 인자인 tz은 타임존을 설정하기 위해서 사용된다.

struct timezone
{
    int tz_minuteswest:  // 그리니치 서측분차  
    int tz_dsttime       // DST 보정타입(일광 절약시간)
}
	

현재 timezone 구조체는 사용되지 않고 있으며, 앞으로도 지원되지 않을 것이다. 간혹 커널 소스등에서 이 필드가 사용되는 경우가 있는데, 모든 경우에 버그로 판단되어서 무시한다. 복잡하게 생각할 필요 없이 tz은 NULL을 사용하도록 한다.

1.3. 반환값

성공하면 0 실패하면 -1을 리턴한다.

1.4. 에러

EFAULT

tv나 tz이 접근할 수 없는 영역을 가리키고 있다.

1.5. 예제

#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main()
{
    struct timeval mytime;

    // 현재 시간을 얻어온다.
    gettimeofday(&mytime, NULL);
    printf("%ld:%ld\n", mytime.tv_sec, mytime.tv_usec);

    // 시간을 1시간 뒤로 되돌려서 설정한다.
    mytime.tv_sec -= 3600;
    settimeofday(&mytime, NULL);
    return 0;
}


1.6. 참고문헌


  1. Unix 시간 다루기

  2.  
  3. time(2)

  4. stime(2)

 

참고 :

+ Recent posts